<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha256-wiz7ZSCn/btzhjKDQBms9Hx4sSeUYsDrTLg7roPstac=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.css" integrity="sha256-6cQIC71/iBIYXFK+0RHAvwmjwWzkWd+r7v/BX3/vZDc=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/green/pace-theme-minimal.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"sumumm.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.19.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":300},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="本文主要是ARM基础知识——ARM的汇编语言相关的基础知识相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:type" content="article">
<meta property="og:title" content="LV08-01-ARM体系-03-ARM汇编基础">
<meta property="og:url" content="https://sumumm.github.io/post/aaf5ec56.html">
<meta property="og:site_name" content="苏木">
<meta property="og:description" content="本文主要是ARM基础知识——ARM的汇编语言相关的基础知识相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV08-ARM%E5%9F%BA%E7%A1%80/LV08-01-ARM%E4%BD%93%E7%B3%BB-03-ARM%E6%B1%87%E7%BC%96%E5%9F%BA%E7%A1%80/img/image-20220728071703224.png">
<meta property="article:published_time" content="2023-07-06T14:59:22.000Z">
<meta property="article:modified_time" content="2025-06-13T16:25:57.001Z">
<meta property="article:author" content="苏木">
<meta property="article:tag" content="LV08-ARM基础">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV08-ARM%E5%9F%BA%E7%A1%80/LV08-01-ARM%E4%BD%93%E7%B3%BB-03-ARM%E6%B1%87%E7%BC%96%E5%9F%BA%E7%A1%80/img/image-20220728071703224.png">


<link rel="canonical" href="https://sumumm.github.io/post/aaf5ec56.html">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://sumumm.github.io/post/aaf5ec56.html","path":"post/aaf5ec56.html","title":"LV08-01-ARM体系-03-ARM汇编基础"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>LV08-01-ARM体系-03-ARM汇编基础 | 苏木</title>
  








    <script src="/js/browser_tools_disable.js"></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<!-- hexo injector head_end start --><link rel="stylesheet" href="https://unpkg.com/hexo-next-tags-plus@latest/lib/tag_plus.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">苏木</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">我的学习之路</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>苏木的家</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类页<span class="badge">42</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档页<span class="badge">673</span></a></li><li class="menu-item menu-item-flink"><a href="/flink/" rel="section"><i class="fa fa-link fa-fw"></i>友人帐</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81GNU%E6%B1%87%E7%BC%96%E8%AF%AD%E6%B3%95"><span class="nav-text">一、GNU汇编语法</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E4%BC%AA%E6%8C%87%E4%BB%A4%E5%92%8C%E4%BC%AA%E6%93%8D%E4%BD%9C"><span class="nav-text">1. 伪指令和伪操作</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-%E4%BC%AA%E6%8C%87%E4%BB%A4"><span class="nav-text">1.1 伪指令</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-%E4%BC%AA%E6%93%8D%E4%BD%9C"><span class="nav-text">1.2 伪操作</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E6%B1%87%E7%BC%96%E8%AF%AD%E6%B3%95%E6%A0%BC%E5%BC%8F"><span class="nav-text">2. 汇编语法格式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E9%A2%84%E5%AE%9A%E4%B9%89%E6%AE%B5"><span class="nav-text">3. 预定义段</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E6%B1%87%E7%BC%96%E7%9A%84%E5%BC%80%E5%A7%8B%E4%B8%8E%E7%BB%93%E6%9D%9F"><span class="nav-text">4. 汇编的开始与结束</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-GNU%E5%87%BD%E6%95%B0%E8%AF%AD%E6%B3%95"><span class="nav-text">5. GNU函数语法</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81%E6%95%B0%E6%8D%AE%E5%A4%84%E7%90%86%E6%8C%87%E4%BB%A4"><span class="nav-text">二、数据处理指令</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E6%95%B0%E6%8D%AE%E5%A4%84%E7%90%86%E6%8C%87%E4%BB%A4%E6%A0%BC%E5%BC%8F"><span class="nav-text">1. 数据处理指令格式</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-%E7%BC%96%E7%A0%81%E6%A0%BC%E5%BC%8F"><span class="nav-text">1.1 编码格式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-%E6%9D%A1%E4%BB%B6%E7%A0%81"><span class="nav-text">1.2 条件码</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-1-%E6%9D%A1%E4%BB%B6%E7%A0%81%E7%A7%8D%E7%B1%BB"><span class="nav-text">1.2.1 条件码种类</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-2-CMP%E6%8C%87%E4%BB%A4"><span class="nav-text">1.2.2 CMP指令</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-3-%E5%AE%9E%E4%BE%8B%E8%AF%B4%E6%98%8E"><span class="nav-text">1.2.3 实例说明</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3-%E7%AB%8B%E5%8D%B3%E6%95%B0"><span class="nav-text">1.3 立即数</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E6%95%B0%E6%8D%AE%E6%90%AC%E7%A7%BB%E6%8C%87%E4%BB%A4"><span class="nav-text">2. 数据搬移指令</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-MOV"><span class="nav-text">2.1 MOV</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-MVN"><span class="nav-text">2.2 MVN</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E6%95%B0%E6%8D%AE%E8%BF%90%E7%AE%97%E6%8C%87%E4%BB%A4"><span class="nav-text">3. 数据运算指令</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1-%E5%8A%A0%E6%B3%95%E8%BF%90%E7%AE%97"><span class="nav-text">3.1 加法运算</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-1-1-ADD"><span class="nav-text">3.1.1 ADD</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-1-2-ADC"><span class="nav-text">3.1.2 ADC</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-1-3-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">3.1.3 使用实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2-%E5%87%8F%E6%B3%95%E8%BF%90%E7%AE%97"><span class="nav-text">3.2 减法运算</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-2-1-SUB"><span class="nav-text">3.2.1 SUB</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-2-2-RSB"><span class="nav-text">3.2.2 RSB</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-1-2-SUC"><span class="nav-text">3.1.2 SUC</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-4-%E4%B9%98%E6%B3%95%E8%BF%90%E7%AE%97"><span class="nav-text">3.4 乘法运算</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-4-1-MUL"><span class="nav-text">3.4.1 MUL</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-5-%E9%99%A4%E6%B3%95%E8%BF%90%E7%AE%97"><span class="nav-text">3.5 除法运算</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-5-1-UDIV"><span class="nav-text">3.5.1 UDIV</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-5-2-SDIV"><span class="nav-text">3.5.2 SDIV</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E9%80%BB%E8%BE%91%E8%BF%90%E7%AE%97%E6%8C%87%E4%BB%A4"><span class="nav-text">4. 逻辑运算指令</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#4-1%E6%8C%89%E4%BD%8D%E4%B8%8E%E8%BF%90%E7%AE%97"><span class="nav-text">4.1按位与运算</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#4-1-1AND"><span class="nav-text">4.1.1AND</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-2-%E6%8C%89%E4%BD%8D%E6%88%96%E8%BF%90%E7%AE%97"><span class="nav-text">4.2 按位或运算</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#4-2-1-ORR"><span class="nav-text">4.2.1 ORR</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-3-%E4%BD%8D%E6%B8%85%E9%99%A4"><span class="nav-text">4.3 位清除</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#4-3-1-BIC"><span class="nav-text">4.3.1 BIC</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-4-%E6%8C%89%E4%BD%8D%E5%BC%82%E6%88%96"><span class="nav-text">4.4 按位异或</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#4-4-1-EOR"><span class="nav-text">4.4.1 EOR</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-5-%E5%B7%A6%E7%A7%BB"><span class="nav-text">4.5 左移</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#4-5-1-LSL"><span class="nav-text">4.5.1 LSL</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-6-%E5%8F%B3%E7%A7%BB"><span class="nav-text">4.6 右移</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#4-6-1-LSR"><span class="nav-text">4.6.1 LSR</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-%E4%B8%89%E7%A7%8D%E7%9B%B8%E5%85%B3%E5%AF%BB%E5%9D%80%E5%AE%9E%E4%BE%8B"><span class="nav-text">5. 三种相关寻址实例</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#5-1-%E7%AB%8B%E5%8D%B3%E6%95%B0%E5%AF%BB%E5%9D%80"><span class="nav-text">5.1 立即数寻址</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-2-%E5%AF%84%E5%AD%98%E5%99%A8%E5%AF%BB%E5%9D%80"><span class="nav-text">5.2 寄存器寻址</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-3-%E5%AF%84%E5%AD%98%E5%99%A8%E7%A7%BB%E4%BD%8D%E5%AF%BB%E5%9D%80"><span class="nav-text">5.3 寄存器移位寻址</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%89%E3%80%81%E8%B7%B3%E8%BD%AC%E6%8C%87%E4%BB%A4"><span class="nav-text">三、跳转指令</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E7%9B%B4%E6%8E%A5%E4%BF%AE%E6%94%B9PC"><span class="nav-text">1. 直接修改PC</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-B"><span class="nav-text">2. B</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-BL"><span class="nav-text">3. BL</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-BX"><span class="nav-text">4. BX</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-BLX"><span class="nav-text">5. BLX</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">6. 使用实例</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%9B%9B%E3%80%81Load-Store%E6%8C%87%E4%BB%A4"><span class="nav-text">四、Load&#x2F;Store指令</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%8D%95%E4%B8%AA%E5%AF%84%E5%AD%98%E5%99%A8%E5%86%85%E5%AD%98%E8%AE%BF%E9%97%AE"><span class="nav-text">1. 单个寄存器内存访问</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-STR"><span class="nav-text">1.1 STR</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-LDR"><span class="nav-text">1.2 LDR</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-1-%E7%94%A8%E6%B3%95%E4%B8%80%EF%BC%9A%E8%AF%BB%E5%8F%96%E6%95%B0%E6%8D%AE"><span class="nav-text">1.2.1 用法一：读取数据</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-2-%E7%94%A8%E6%B3%95%E4%BA%8C%EF%BC%9A%E4%BC%AA%E6%8C%87%E4%BB%A4"><span class="nav-text">1.2.2 用法二：伪指令</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3-%E4%B8%A4%E7%A7%8D%E7%9B%B8%E5%85%B3%E5%AF%BB%E5%9D%80%E6%96%B9%E5%BC%8F%E5%AE%9E%E4%BE%8B"><span class="nav-text">1.3 两种相关寻址方式实例</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-3-1-%E5%AF%84%E5%AD%98%E5%99%A8%E9%97%B4%E6%8E%A5%E5%AF%BB%E5%9D%80"><span class="nav-text">1.3.1 寄存器间接寻址</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-3-2-%E5%AF%84%E5%AD%98%E5%99%A8%E5%9F%BA%E5%9D%80%E5%8F%98%E5%9D%80%E5%AF%BB%E5%9D%80"><span class="nav-text">1.3.2 寄存器基址变址寻址</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%A4%9A%E5%AF%84%E5%AD%98%E5%99%A8%E5%86%85%E5%AD%98%E8%AE%BF%E9%97%AE"><span class="nav-text">2. 多寄存器内存访问</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-STM"><span class="nav-text">2.1 STM</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-LDM"><span class="nav-text">2.2 LDM</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-%E4%B8%80%E7%A7%8D%E7%9B%B8%E5%85%B3%E5%AF%BB%E5%9D%80%E6%96%B9%E5%BC%8F%E5%AE%9E%E4%BE%8B"><span class="nav-text">2.3 一种相关寻址方式实例</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-3-1-%E5%A4%9A%E5%AF%84%E5%AD%98%E5%99%A8%E5%AF%BB%E5%9D%80"><span class="nav-text">2.3.1 多寄存器寻址</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E6%A0%88"><span class="nav-text">3. 栈</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1-%E6%A0%88%E7%9A%84%E6%A6%82%E5%BF%B5"><span class="nav-text">3.1 栈的概念</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2-%E6%A0%88%E7%9A%84%E5%88%86%E7%B1%BB"><span class="nav-text">3.2 栈的分类</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-3-%E6%A0%88%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="nav-text">3.3 栈的使用</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-3-1-%E5%8F%B6%E5%AD%90%E5%87%BD%E6%95%B0"><span class="nav-text">3.3.1 叶子函数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-3-2-%E9%9D%9E%E5%8F%B6%E5%AD%90%E5%87%BD%E6%95%B0"><span class="nav-text">3.3.2 非叶子函数</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%94%E3%80%81%E7%8A%B6%E6%80%81%E5%AF%84%E5%AD%98%E5%99%A8%E4%BC%A0%E9%80%81%E6%8C%87%E4%BB%A4"><span class="nav-text">五、状态寄存器传送指令</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-MRS"><span class="nav-text">1. MRS</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-MSR"><span class="nav-text">2. MSR</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%85%AD%E3%80%81%E8%BD%AF%E4%B8%AD%E6%96%AD%E6%8C%87%E4%BB%A4"><span class="nav-text">六、软中断指令</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-SWI"><span class="nav-text">1. SWI</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%83%E3%80%81%E5%8D%8F%E5%A4%84%E7%90%86%E5%99%A8%E6%8C%87%E4%BB%A4"><span class="nav-text">七、协处理器指令</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%8D%8F%E5%A4%84%E7%90%86%E5%99%A8%E7%AE%80%E4%BB%8B"><span class="nav-text">1. 协处理器简介</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%8D%8F%E5%A4%84%E7%90%86%E5%99%A8%E6%8C%87%E4%BB%A4%E7%AE%80%E4%BB%8B"><span class="nav-text">2. 协处理器指令简介</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-%E5%8D%8F%E5%A4%84%E7%90%86%E5%99%A8%E6%95%B0%E6%8D%AE%E8%BF%90%E7%AE%97%E6%8C%87%E4%BB%A4"><span class="nav-text">2.1 协处理器数据运算指令</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-1-1-CDP"><span class="nav-text">2.1.1 CDP</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-%E5%8D%8F%E5%A4%84%E7%90%86%E5%99%A8%E5%AD%98%E5%82%A8%E5%99%A8%E8%AE%BF%E9%97%AE%E6%8C%87%E4%BB%A4"><span class="nav-text">2.2 协处理器存储器访问指令</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2-1-LDC"><span class="nav-text">2.2.1 LDC</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2-2-STC"><span class="nav-text">2.2.2 STC</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-%E5%8D%8F%E5%A4%84%E7%90%86%E5%99%A8%E5%AF%84%E5%AD%98%E5%99%A8%E8%AE%BF%E9%97%AE%E6%8C%87%E4%BB%A4"><span class="nav-text">2.3 协处理器寄存器访问指令</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-3-1-MRC"><span class="nav-text">2.3.1 MRC</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-3-2-MCR"><span class="nav-text">2.3.2 MCR</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%85%AB%E3%80%81%E4%BC%AA%E6%8C%87%E4%BB%A4"><span class="nav-text">八、伪指令</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-NOP"><span class="nav-text">1. NOP</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-LDR"><span class="nav-text">2. LDR</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B9%9D%E3%80%81%E4%BC%AA%E6%93%8D%E4%BD%9C"><span class="nav-text">九、伪操作</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-byte"><span class="nav-text">1. .byte</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-short"><span class="nav-text">2. .short</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-long"><span class="nav-text">3. .long</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-equ"><span class="nav-text">4. .equ</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-align"><span class="nav-text">5. .align</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-end"><span class="nav-text">6. .end</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#7-global"><span class="nav-text">7. .global</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#8-local"><span class="nav-text">8. .local</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#9-weak"><span class="nav-text">9. .weak</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#10-word"><span class="nav-text">10. .word</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#11-space"><span class="nav-text">11. .space</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#12-if"><span class="nav-text">12. .if </span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#13-macro"><span class="nav-text">13. .macro</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#14-rept"><span class="nav-text">14. .rept</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%8D%81%E3%80%81C%E4%B8%8E%E6%B1%87%E7%BC%96%E6%B7%B7%E5%90%88%E7%BC%96%E7%A8%8B"><span class="nav-text">十、C与汇编混合编程</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E6%B1%87%E7%BC%96%E4%B8%AD%E8%B0%83%E7%94%A8C"><span class="nav-text">1. 汇编中调用C</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-C%E4%B8%AD%E8%B0%83%E7%94%A8%E6%B1%87%E7%BC%96"><span class="nav-text">2. C中调用汇编</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-C%E5%86%85%E5%B5%8C%E6%B1%87%E7%BC%96"><span class="nav-text">3. C内嵌汇编</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-ATPCS%E5%8D%8F%E8%AE%AE"><span class="nav-text">4. ATPCS协议</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#4-1-ATPCS%E7%AE%80%E4%BB%8B"><span class="nav-text">4.1 ATPCS简介</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-2-ATPCS%E4%B8%BB%E8%A6%81%E5%86%85%E5%AE%B9"><span class="nav-text">4.2 ATPCS主要内容</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#4-2-1-%E5%AF%84%E5%AD%98%E5%99%A8%E4%BD%BF%E7%94%A8%E8%A7%84%E5%88%99"><span class="nav-text">4.2.1 寄存器使用规则</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-2-2-%E5%A0%86%E6%A0%88%E8%A7%84%E5%88%99"><span class="nav-text">4.2.2 堆栈规则</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-2-3-%E5%8F%82%E6%95%B0%E4%BC%A0%E9%80%92%E8%A7%84%E5%88%99"><span class="nav-text">4.2.3 参数传递规则</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-2-4-%E5%AD%90%E7%A8%8B%E5%BA%8F%E7%BB%93%E6%9E%9C%E8%BF%94%E5%9B%9E%E8%A7%84%E5%88%99"><span class="nav-text">4.2.4 子程序结果返回规则</span></a></li></ol></li></ol></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="苏木"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">苏木</p>
  <div class="site-description" itemprop="description">莫道桑榆晚，为霞尚满天</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">673</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">42</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">43</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/sumumm" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;sumumm" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://sumumm.github.io/post/aaf5ec56.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="苏木">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏木">
      <meta itemprop="description" content="莫道桑榆晚，为霞尚满天">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="LV08-01-ARM体系-03-ARM汇编基础 | 苏木">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          LV08-01-ARM体系-03-ARM汇编基础
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2023-07-06 22:59:22" itemprop="dateCreated datePublished" datetime="2023-07-06T22:59:22+08:00">2023-07-06</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">嵌入式开发</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/" itemprop="url" rel="index"><span itemprop="name">01HQ课程体系</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV08-ARM%E5%9F%BA%E7%A1%80/" itemprop="url" rel="index"><span itemprop="name">LV08-ARM基础</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>16k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>1:01</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>本文主要是ARM基础知识——ARM的汇编语言相关的基础知识相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。</p>
<span id="more"></span>

<!-- Photo: https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV08-ARM%E5%9F%BA%E7%A1%80/LV08-01-ARM%E4%BD%93%E7%B3%BB-03-ARM%E6%B1%87%E7%BC%96%E5%9F%BA%E7%A1%80/img/ -->

<details class="folding-tag" blue><summary> 点击查看使用工具及版本 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" width=150px>Windows</td>        <td align="left">windows11</td>    </tr>    <tr>        <td align="center">Ubuntu</td>        <td align="left">Ubuntu16.04的64位版本</td>      </tr>    <tr>        <td align="center">VMware® Workstation 16 Pro</td>        <td align="left">16.2.3 build-19376536</td>      </tr>    <tr>        <td align="center">SecureCRT</td>        <td align="left">Version 8.7.2 (x64 build 2214)   -   正式版-2020年5月14日</td>      </tr>    <tr>        <td align="center">开发板</td>        <td align="left">正点原子 i.MX6ULL Linux阿尔法开发板</td>      </tr>    <tr>        <td align="center">uboot</td>        <td align="left">NXP官方提供的uboot，NXP提供的版本为uboot-imx-rel_imx_4.1.15_2.1.0_ga(使用的uboot版本为U-Boot 2016.03)</td>      </tr>    <tr>        <td align="center">linux内核</td>        <td align="left">linux-4.15(NXP官方提供)</td>      </tr>    <tr>        <td align="center">STM32开发板</td>        <td align="left">正点原子战舰V3(STM32F103ZET6)</td>      </tr>    <tr>        <td align="center" width=150>Keil(MDK)</td>        <td align="left">Keil uVision V4.54.0.0</td>      </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看本文参考资料 </summary>
              <div class='content'>
              <table>    <tr><td align="center">参考方向  </td><td align="center">参考原文</td></tr>    <tr><td align="left">驱动开发指南</td><td align="left"><a href="http://47.111.11.73/docs/boards/arm-linux/zdyz-i.mx6ull.html#i-mx6ull-linux" target="_blank">i.MX6ULL Linux阿尔法开发板资料 <i class="fa fa-external-link-alt"></i> </a></td></tr>    <tr><td align="left">ATPCS</td><td align="left"><a href="https://www.softool.cn/blog-8.html" target="_blank">ATPCS 规则简介 (softool.cn)  <i class="fa fa-external-link-alt"></i> </a></td></tr>    <tr><td align="left">ARM汇编指令</td><td align="left"><a href="https://developer.arm.com/documentation/ddi0406/cd/?lang=en" target="_blank">ARM Architecture Reference Manual ARMv7-A and ARMv7-R edition  <i class="fa fa-external-link-alt"></i> </a></td></tr>    <tr><td align="left">ARM汇编指令格式</td><td align="left"><a href="https://blog.csdn.net/happylzs2008/article/details/106171314" target="_blank">arm指令一般编码格式和一般语法格式  <i class="fa fa-external-link-alt"></i> </a></td></tr>    <tr><td align="left">立即数判断</td><td align="left"><a href="https://blog.csdn.net/u011118276/article/details/44924867" target="_blank">ARM指令中如何判断一个立即数是有效立即数  <i class="fa fa-external-link-alt"></i> </a></td></tr>    <tr><td align="left">立即数</td><td align="left"><a href="https://blog.csdn.net/a99778800/article/details/6759825" target="_blank">浅述ARM汇编的立即数_FL智能的博客-CSDN博客_arm立即数  <i class="fa fa-external-link-alt"></i> </a></td></tr>    <tr><td align="left">协处理器指令</td><td align="left"><a href="https://www.86ic.net/fangan/fpga/276387.html" target="_blank">ARM协处理器主要用途 及其 指令CDP LDC STC MCR MRC介绍 - 86IC科技网  <i class="fa fa-external-link-alt"></i> </a></td></tr>    <tr><td align="left">CP15</td><td align="left"><a href="https://blog.csdn.net/liangzc1124/article/details/112413742" target="_blank">ARM协处理器CP15介绍_Leon_George的博客-CSDN博客_arm cp15  <i class="fa fa-external-link-alt"></i> </a></td></tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看相关文件下载 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center">---</td>        <td align="left">--- <a href="" target="_blank">  <i class="fa fa-external-link-alt"></i></a></td>      </tr></table>
              </div>
            </details>

<blockquote>
<p>为什么要学ARM汇编？</p>
</blockquote>
<p>我们在学习 STM32的时候几乎没有用到过汇编，但是我们在进行嵌入式<code>Linux</code>开发的时候是绝对要掌握基本的 <code>ARM </code>汇编，对于<code>Cortex-A</code>芯片来讲，大部分芯片在上电以后 <code>C</code>语言环境还没准备好，所以第一行程序肯定是汇编的，至于要写多少汇编程序，那就看我们能在哪一步把 <code>C</code>语言环境准备好。</p>
<p>所谓的 <code>C </code>语言环境就是保证 <code>C</code> 语言能够正常运行。 <code>C</code>语言中的函数调用涉及到出栈入栈，出栈入栈就要对堆栈进行操作，所谓的堆栈其实就是一段内存，这段内存比较特殊，由 <code>SP</code> 指针访问， <code>SP</code> 指针指向栈顶。芯片一上电 <code>SP</code> 指针还没有初始化，所以 <code>C</code> 语言没法运行，对于有些芯片还需要初始化 <code>DDR</code>，因为芯片本身没有 <code>RAM</code>，或者内部 <code>RAM</code>不开放给用户使用，用户代码需要在<code>DDR</code>中运行，因此一开始要用汇编来初始化 <code>DDR</code>控制器。后面学习 <code>Uboot</code>和 <code>Linux</code> 内核的时候汇编是必须要会的。</p>
<blockquote>
<p>官方文档？</p>
</blockquote>
<p>我们在<code>ARM</code>的官网上可以找到很多的芯片手册以及编程指南，关于这篇笔记中的<code>ARM</code>指令，我们可以参考这篇文档：《<a target="_blank" rel="noopener" href="https://developer.arm.com/documentation/ddi0406/cd/?lang=en">ARM Architecture Reference Manual ARMv7-A and ARMv7-R edition</a>》的<code>A4</code>章节，我们可以在这一章节系统的了解到<code>Cotex-A7</code>的所有汇编指令。我使用的开发板使用的<code>Cotex-A</code>系列的<code>ARM</code>处理器，所以这份文档就可以了。</p>
<h1 id="一、GNU汇编语法"><a href="#一、GNU汇编语法" class="headerlink" title="一、GNU汇编语法"></a><font size=3>一、<code>GNU</code>汇编语法</font></h1><h2 id="1-伪指令和伪操作"><a href="#1-伪指令和伪操作" class="headerlink" title="1. 伪指令和伪操作"></a><font size=3>1. 伪指令和伪操作</font></h2><h3 id="1-1-伪指令"><a href="#1-1-伪指令" class="headerlink" title="1.1 伪指令"></a><font size=3>1.1 伪指令</font></h3><p>伪指令(<code>Pseudo Instruction</code>)是用于<strong>对汇编过程进行控制的指令</strong>，该类指令并<strong>不是可执行指令</strong>，<strong>没有机器代码</strong>，只用于汇编过程中为汇编程序提供汇编信息。例如，提供如下信息：哪些是指令、哪些是数据及数据的字长、程序的起始地址和结束地址等。伪指令有<code>2</code>个特点：</p>
<p>（1）由于是伪“指令”，因而它只存在于汇编语言中。高级语言中不叫指令，叫语句；</p>
<p>（2）由于是“伪”指令，也即“假”指令，因而不是可执行指令，不会产生机器代码，不会占用<code>ROM</code>空间，只用于汇编过程中为汇编程序提供汇编信息。</p>
<h3 id="1-2-伪操作"><a href="#1-2-伪操作" class="headerlink" title="1.2 伪操作"></a><font size=3>1.2 伪操作</font></h3><p>汇编语言程序语句除指令以外还可以由伪操作和宏指令组成。伪操作不像机器指令那样是在程序运行期间由<code>CPU</code>来执行的，它是在汇编程序对源程序汇编期间由汇编程序处理的操作，它们可以完成如数据定义、分配存储区、指示程序结束等功能。</p>
<h2 id="2-汇编语法格式"><a href="#2-汇编语法格式" class="headerlink" title="2. 汇编语法格式"></a><font size=3>2. 汇编语法格式</font></h2><p><code>GNU</code>汇编语法适用于所有的架构，并不是 <code>ARM</code> 独享的， <code>GNU</code>汇编由一系列的语句组成，每行一条语句，每条语句有三个可选部分：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">label: instruction @ comment</span><br></pre></td></tr></table></figure>

<p><strong>【语句说明】</strong></p>
<ul>
<li><p><code>label</code>：标号，表示地址位置，有些指令前面可能会有标号，这样就可以通过这个标号得到指令的地址，标号也可以用来表示数据地址。注意 label后面的<code>:</code>，任何以<code>:</code>结尾的标识符都会被识别为 一个标号。</p>
</li>
<li><p><code>instruction</code>：指令，也就是汇编指令或伪指令。</p>
</li>
<li><p><code>@</code>：表示后面的是注释，就跟 <code>C</code>语言里面的“<code>/*</code>和 <code>*/</code>一样，其实在 <code>GNU</code>汇编文件中我们也可以使用<code>/*</code>和 <code>*/</code>来注释。</p>
</li>
<li><p><code>comment</code>：就是注释的内容了。</p>
</li>
</ul>
<p><strong>【注意】</strong></p>
<p>（1）<code>ARM</code>中的指令、伪指令、伪操作、寄存器名等可以全部使用大写，也可以全部使用小写，但是不能大小写混用。</p>
<p>（2）汇编指令每行结束不需要带<code>;</code>或者其他任何符号。</p>
<h2 id="3-预定义段"><a href="#3-预定义段" class="headerlink" title="3. 预定义段"></a><font size=3>3. 预定义段</font></h2><p>我们可以使用以下伪操作来定义一个段：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">.section .UserSection @定义一个 UserSection段</span><br></pre></td></tr></table></figure>

<p>在汇编系统中预定义了一些段名：</p>
<table>
    <tr><td align="left" width=150px>.text   </td><td align="left">表示代码段。</td></tr>
    <tr><td align="left" width=150px>.data   </td><td align="left">初始化的数据段。</td></tr>
    <tr><td align="left" width=150px>.bss    </td><td align="left">未初始化的数据段。</td></tr>
    <tr><td align="left" width=150px>.rodata </td><td align="left">只读数据段。</td></tr>
</table>

<h2 id="4-汇编的开始与结束"><a href="#4-汇编的开始与结束" class="headerlink" title="4. 汇编的开始与结束"></a><font size=3>4. 汇编的开始与结束</font></h2><p>汇编程序的默认入口标号是 <code>_start</code>，不过我们也可以在链接脚本中使用 <code>ENTRY</code>来指明其它的入口点，一般还是使用 <code>_start</code>作为入口标号，下边就是一个完整的<code>GNU</code>汇编程序开始与结束的格式：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">.text				@ 表示当前段为代码段 </span><br><span class="line">.global _start		@ 声明_start为全局符号</span><br><span class="line">_start:				@ 汇编程序的入口</span><br><span class="line"></span><br><span class="line">	@ 中间这里是我们要写的指令</span><br><span class="line">	</span><br><span class="line">STOP:				@ 死循环，防止程序跑飞</span><br><span class="line">	B STOP</span><br><span class="line">	</span><br><span class="line">.end				@ 汇编程序的结束</span><br></pre></td></tr></table></figure>

<h2 id="5-GNU函数语法"><a href="#5-GNU函数语法" class="headerlink" title="5. GNU函数语法"></a><font size=3>5. <code>GNU</code>函数语法</font></h2><p>在汇编中同时也支持函数的定义使用，一般定义格式如下：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">函数名 : </span><br><span class="line">	函数体</span><br><span class="line">	返回语句</span><br></pre></td></tr></table></figure>

<p><strong>【注意】</strong><code>GNU</code>汇编函数返回语句不是必须的。</p>
<details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">/* 未定义中断 */ </span><br><span class="line">Undefined_Handler:                 @ 函数名</span><br><span class="line">	LDR R0, = Undefined_Handler    @ 函数体</span><br><span class="line">	BX R0                          @ 返回语句，并非必须</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h1 id="二、数据处理指令"><a href="#二、数据处理指令" class="headerlink" title="二、数据处理指令"></a><font size=3>二、数据处理指令</font></h1><h2 id="1-数据处理指令格式"><a href="#1-数据处理指令格式" class="headerlink" title="1. 数据处理指令格式"></a><font size=3>1. 数据处理指令格式</font></h2><h3 id="1-1-编码格式"><a href="#1-1-编码格式" class="headerlink" title="1.1 编码格式"></a><font size=3>1.1 编码格式</font></h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;opcode&gt; &#123;cond&#125; &#123;S&#125; &lt;Rd&gt;,&lt;Rn&gt;,&lt;shifter_operand&gt;</span><br></pre></td></tr></table></figure>

<p><strong>【格式说明】</strong></p>
<ul>
<li><code>opcode</code>：指令助记符，如<code>MOV</code>等；</li>
<li><code>cond</code>：条件码助记符，如<code>EQ(0000)</code>、<code>NE(0001)</code>)等;</li>
<li><code> S</code>：如果指令有<code>S</code>后缀，则该指令的操作会影响<code>CPSR</code>的值；</li>
<li><code>Rd</code>：目标寄存器；</li>
<li><code>Rn</code>：包含第一个源操作数的寄存器；</li>
<li><code>shifter_operand</code>:  表示第二个源操作数，可以为立即数。</li>
</ul>
<p><strong>【格式图示】</strong>在网上查询了下，一般来说<code>ARM</code>数据处理指令编码格式如下图：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV08-ARM%E5%9F%BA%E7%A1%80/LV08-01-ARM%E4%BD%93%E7%B3%BB-03-ARM%E6%B1%87%E7%BC%96%E5%9F%BA%E7%A1%80/img/image-20220728071703224.png" alt="image-20220728071703224" style="zoom:50%;" />

<p><strong>【注意】</strong>对于<code>ARM</code>的<code>Cotex-A7</code>系列的指令编码格式可以看这篇文档《<a target="_blank" rel="noopener" href="https://developer.arm.com/documentation/ddi0406/cd/?lang=en">ARM Architecture Reference Manual ARMv7-A and ARMv7-R edition</a>》的<code>A5</code>章节</p>
<h3 id="1-2-条件码"><a href="#1-2-条件码" class="headerlink" title="1.2 条件码"></a><font size=3>1.2 条件码</font></h3><h4 id="1-2-1-条件码种类"><a href="#1-2-1-条件码种类" class="headerlink" title="1.2.1 条件码种类"></a><font size=3>1.2.1 条件码种类</font></h4><p>当处理器工作在<code>ARM</code>状态时，几乎所有的指令均根据<code>CPSR</code>中条件码的状态和指令的条件域有条件的执行。当指令的执行条件满足时，指令被执行，否则指令被忽略。每一条<code>ARM</code>指令包含<code>4</code>位的条件码，位于指令的最高<code>4</code>位<code>[31:28]</code>。</p>
<p>条件码共有<code>16</code>种，每种条件码可用两个字符表示，这两个字符可以添加在指令助记符的后面和指令同时使用。例如，跳转指令B可以加上后缀<code>EQ</code>变为<code>BEQ</code>表示相等则跳转，即当<code>CPSR</code>中的<code>Z</code>标志置位时发生跳转。</p>
<table>
    <tr><td align="center">助记符</td><td align="center">含义</td><td align="center">条件码</td><td align="center">CPSR变化标志</td></tr>
    <tr><td align="center">EQ</td><td align="left">相等</td><td align="center">0000</td><td align="left">Z==1</td></tr>
    <tr><td align="center">NE</td><td align="left">不相等</td><td align="center">0001</td><td align="left">Z==0</td></tr>
    <tr><td align="center">CS</td><td align="left">无符号大于或等于	</td><td align="center">0010</td><td align="left">C==1</td></tr>
    <tr><td align="center">CC</td><td align="left">无符号小于</td><td align="center">0011</td><td align="left">C==0</td></tr>
    <tr><td align="center">MI</td><td align="left">负数</td><td align="center">0100</td><td align="left">N==1</td></tr>
    <tr><td align="center">PL</td><td align="left">正数或零</td><td align="center">0101</td><td align="left">N==0</td></tr>
    <tr><td align="center">VS</td><td align="left">溢出</td><td align="center">0110</td><td align="left">V==1</td></tr>
    <tr><td align="center">VC</td><td align="left">未溢出</td><td align="center">0111</td><td align="left">V==0</td></tr>
    <tr><td align="center">HI</td><td align="left">无符号大于</td><td align="center">1000</td><td align="left">C==1 Z==0</td></tr>
    <tr><td align="center">LS</td><td align="left">无符号小于或等于	</td><td align="center">1001</td><td align="left">C==0 Z==1</td></tr>
    <tr><td align="center">GE</td><td align="left">带符号大于或等于	</td><td align="center">1010</td><td align="left">N==V</td></tr>
    <tr><td align="center">LT</td><td align="left">带符号小于</td><td align="center">1011</td><td align="left">N!=V</td></tr>
    <tr><td align="center">GT</td><td align="left">带符号大于</td><td align="center">1100</td><td align="left">Z==0 N==V</td></tr>
    <tr><td align="center">LE</td><td align="left">带符号小于或等于	</td><td align="center">1101</td><td align="left">Z==1 N!=V</td></tr>
    <tr><td align="center">AL</td><td align="left">无条件执行</td><td align="center">1110</td><td align="left">忽略</td></tr>
</table>


<h4 id="1-2-2-CMP指令"><a href="#1-2-2-CMP指令" class="headerlink" title="1.2.2 CMP指令"></a><font size=3>1.2.2 <code>CMP</code>指令</font></h4><p>条件码的使用是需要有<code>CPSR</code>寄存器相关位变化的，这个时候我们就需要使用<code>CMP</code>指令来把一个寄存器的数据和另一个寄存器的数据或一个立即数进行比较，同时更新<code>CPSR</code>中条件标志位的值。<code>CMP</code>指令的格式如下：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">CMP Rd, Rn          @ 相当于 Rd - Rn </span><br><span class="line">CMP Rd, #immediate  @ 相当于 Rd - immediate， immediate表示立即数，后边会学到</span><br></pre></td></tr></table></figure>

<p>实际上，<code>CMP</code>指令所做的事情就是将<code>Rd</code>和<code>Rn</code>相减，这实质上就是一个减法指令，但是运算的结果不会存入寄存器，只会通过<code>CPSR</code>的相关标志位表示出来。</p>
<h4 id="1-2-3-实例说明"><a href="#1-2-3-实例说明" class="headerlink" title="1.2.3 实例说明"></a><font size=3>1.2.3 实例说明</font></h4><p>后边我们将学习到的大多数指令都可以加上条件码，例如<code>SUB</code>、<code>B</code>等。这里放一个实例，具体的其他指令后边会学习到：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">.text				@ 表示当前段为代码段 </span><br><span class="line">.global _start		@ 声明_start为全局符号</span><br><span class="line">_start:				@ 汇编程序的入口</span><br><span class="line">	MOV R0, #6</span><br><span class="line">	MOV R1, #15</span><br><span class="line">START:</span><br><span class="line">	CMP R0, R1</span><br><span class="line">	BEQ STOP         @ R0 == R1， 程序退出</span><br><span class="line">	SUBGT R0, R0, R1 @ R0 &gt;  R1， R0 = R0 - R1</span><br><span class="line">	SUBLT R1, R1, R0 @ R0 &lt;  R1， R1 = R1 - R0</span><br><span class="line">	B START</span><br><span class="line">	</span><br><span class="line">STOP:				@ 死循环，防止程序跑飞</span><br><span class="line">	B STOP</span><br><span class="line">	</span><br><span class="line">.end				@ 汇编程序的结束</span><br></pre></td></tr></table></figure>

<p>这段汇编表示的含义用<code>C</code>语言表示出来就是下边的意思：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> a = <span class="number">6</span>;</span><br><span class="line">    <span class="type">int</span> b = <span class="number">15</span>;</span><br><span class="line">    <span class="keyword">while</span>(<span class="number">1</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">if</span>(a == b) <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">if</span>(a &gt; b) a = a - b;</span><br><span class="line">        <span class="keyword">if</span>(a &lt; b) b = b - a;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="1-3-立即数"><a href="#1-3-立即数" class="headerlink" title="1.3 立即数"></a><font size=3>1.3 立即数</font></h3><p>在学习指令之前，需要了解一下<strong>立即数</strong>。关于立即数，百度百科是这样解释的：<a target="_blank" rel="noopener" href="https://baike.baidu.com/item/%E7%AB%8B%E5%8D%B3%E6%95%B0/3062020">立即数_</a>通常是指在立即寻址方式指令中给出的数。可以是<code>8</code>位、<code>16</code>位或<code>32</code>位，该数值紧跟在<a target="_blank" rel="noopener" href="https://baike.baidu.com/item/%E6%93%8D%E4%BD%9C%E7%A0%81/3220418">操作码</a>之后。</p>
<p>其实看完后不是很理解，后来查了其他的一些资料，发现立即数可以和汇编指令存储在一起，在汇编之后会和指令共同构成机器码。而且并不是随便什么数都可以是立即数，例如<code>0x4000000</code>就是一个立即数，但是<code>0x12345678</code>就不是立即数，当我们直接使用<code>MOV</code>搬移非立即数时，会报以下错误：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">error: immediate expression requires a # prefix -- `mov R0,0x12345678&#x27;</span><br></pre></td></tr></table></figure>

<p>在<code>ARM</code>汇编指令中，<code>MOV</code>指令将会将常用到立即数，立即数有如下语法格式：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;immediate&gt; = immed_8 循环右移 (2 * rotate_imm)</span><br></pre></td></tr></table></figure>

<p><strong>【格式说明】</strong></p>
<ul>
<li><code>immediate</code>：表示有效的立即数；</li>
<li><code>immed_8</code>：表示一个普通的<code>8</code>位常数；</li>
<li><code>rotate_imm</code>：表示循环右移的位数，和<code>2</code>相乘之后就变成了偶数次位移(偶数表示的时候可以用<code>2n</code>，所以这里也很好理解)。</li>
</ul>
<p><strong>【说明】</strong>上边的格式就决定了立即数必须可以由一个<code>8</code>位常数循环右移偶数位得到。</p>
<blockquote>
<p>为什么要这样？</p>
</blockquote>
<p>我们可以看一下<code>shifter_operand</code>所占的位数是<code>12</code>位。要用一个<code>12</code>位的编码来表示任意的<code>32</code>位数是绝对不可能的。但是又要用<code>12</code>位的编码来表示<code>32</code>位数，这又该怎么办呢？那就只有在表示数的数量上做限制。通过编码来实现用<code>12</code>位的编码来表示<code>32</code>位数。于是在<code>12</code>位的<code>shifter_operand</code>中：<code>8</code>位存数据，<code>4</code>位存移位的次数，这样就导致了立即数有了限制。</p>
<details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <ul><li><code>0x104</code></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">immediate  : 0x104 @ 0000 0000 0000 0000   0000 0001 0000 0100</span><br><span class="line">immed_8    : 0x41  @ 0000 0000 0000 0000   0000 0000 0100 0001 </span><br><span class="line">rotate_imm : 15    @ 将会循环右移2x15=30位</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h2 id="2-数据搬移指令"><a href="#2-数据搬移指令" class="headerlink" title="2. 数据搬移指令"></a><font size=3>2. 数据搬移指令</font></h2><h3 id="2-1-MOV"><a href="#2-1-MOV" class="headerlink" title="2.1 MOV"></a><font size=3>2.1 <code>MOV</code></font></h3><p><code>MOV</code>指令用于将数据从一个寄存器拷贝到另外一个寄存器，或者将一个<strong>立即数</strong>传递到寄存器里面，使用示例如下：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">MOV R1, R0</span><br><span class="line">MOV R2, #3</span><br><span class="line">MOV R2, #0xFF</span><br></pre></td></tr></table></figure>

<p><strong>【注意】</strong>这里我们通过<code>MOV</code>指令，只能将一个立即数存入寄存器，那如果想存任意<code>32</code>位数据据呢？里边肯定有很多都不是立即数，这个时候我们可以用<code>LDR</code>指令，后边会学习到。</p>
<h3 id="2-2-MVN"><a href="#2-2-MVN" class="headerlink" title="2.2 MVN"></a><font size=3>2.2 <code>MVN</code></font></h3><p><code>MVN</code>指令用于将一个数据按位取反后从一个寄存器拷贝到年另一个寄存器，或者将一个立即数按位取反后传递到寄存器中：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">MVN R4, R0</span><br><span class="line">MVN R4, #0xFF	@ 将数据按位取反后放入寄存器R4</span><br></pre></td></tr></table></figure>

<h2 id="3-数据运算指令"><a href="#3-数据运算指令" class="headerlink" title="3. 数据运算指令"></a><font size=3>3. 数据运算指令</font></h2><p>一般数据运算指令都符合以下格式：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">操作码 目标寄存器, 第一操作寄存器，第二操作数</span><br></pre></td></tr></table></figure>

<ul>
<li>操作码：就是表示执行哪种操作；</li>
<li>目标寄存器：用于存储目标寄存器；</li>
<li>第一操作寄存器 ：存储第一个参数运算的数据，这里不可以是一个立即数，<strong>只能写寄存器</strong>；</li>
<li>第二操作数：第二个参与运算的数据，<strong>可以是立即数，也可以是寄存器</strong>。</li>
</ul>
<h3 id="3-1-加法运算"><a href="#3-1-加法运算" class="headerlink" title="3.1 加法运算"></a><font size=3>3.1 加法运算</font></h3><h4 id="3-1-1-ADD"><a href="#3-1-1-ADD" class="headerlink" title="3.1.1 ADD"></a><font size=3>3.1.1 <code>ADD</code></font></h4><table>
    <tr><td align="center">指令</td><td align="center">计算公式 </td></tr>
    <tr><td align="left">ADD Rd, Rn, Rm</td><td align="left">Rd = Rn + Rm</td></tr>
    <tr><td align="left">ADD Rd, Rn, #immediate</td><td align="left">Rd = Rn + #immediate</td></tr>
</table>


<p><strong>【指令说明】</strong><code>ADD</code>是<strong>不带进位</strong>的加法指令。</p>
<p><strong>【使用实例】</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">MOV R1, #1</span><br><span class="line">MOV R2, #2</span><br><span class="line"></span><br><span class="line">ADD R3, R1, R2  @ R3 = R1 + R2 = 3</span><br><span class="line">ADD R3, R2, #3  @ R3 = R2 + 3 = 5</span><br></pre></td></tr></table></figure>

<h4 id="3-1-2-ADC"><a href="#3-1-2-ADC" class="headerlink" title="3.1.2 ADC"></a><font size=3>3.1.2 <code>ADC</code></font></h4><table>
    <tr><td align="center">指令</td><td align="center">计算公式 </td></tr>
    <tr><td align="left">ADC Rd, Rn, Rm</td><td align="left">Rd = Rn + Rm + 进位</td></tr>
    <tr><td align="left">ADC Rd, Rn, #immediate</td><td align="left">Rd = Rn + #immediate + 进位</td></tr>
</table>


<p><strong>【指令说明】</strong><code>ADC</code>是<strong>带进位</strong>的加法指令。</p>
<p><strong>【注意】</strong>进行带进位的加法指令时，若有用到<code>ADD</code>，则需要加上<code>S</code>后缀，这样才会使<code>CPSR</code>进位标志<code>C</code>产生变化，才可以真正加上进位。</p>
<p><strong>【使用实例】</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">@ 6.1第一个数 0000 0002 FFFF FFFF</span><br><span class="line">MOV R2, #0xFFFFFFFF                     @ 第一个数低32位</span><br><span class="line">MOV R3, #0x00000002                     @ 第一个数高32位</span><br><span class="line">@ 6.2第二个数 0000 0004 0000 0003</span><br><span class="line">MOV R4, #0x00000003                     @ 第二个数低32位</span><br><span class="line">MOV R5, #0x00000004                     @ 第二个数高32位</span><br><span class="line">@ 6.3不带进位加法结果     0000 0006 0000 0010</span><br><span class="line">ADD R6, R2, R4                          @ 结果的低32位</span><br><span class="line">ADD R7, R3, R5                          @ 结果的高32位</span><br><span class="line">@ 6.4带进位加法结果       0000 0007 0000 0010</span><br><span class="line">ADDS R6, R2, R4                         @ 结果的低32位</span><br><span class="line">ADC R7, R3, R5                          @ 结果的高32位(R7 = R3 + R5 + CPSR[C])</span><br></pre></td></tr></table></figure>

<h4 id="3-1-3-使用实例"><a href="#3-1-3-使用实例" class="headerlink" title="3.1.3 使用实例"></a><font size=3>3.1.3 使用实例</font></h4><blockquote>
<p>【题目】使用ARM寄存器完成两个128位数的加法运算</p>
</blockquote>
<p>【解答】</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">@ 1.1第一个数 0000 0002 FFFF FFFF 0000 0002 FFFF FFFF</span><br><span class="line">MOV R0, #0xFFFFFFFF</span><br><span class="line">MOV R1, #0x00000002</span><br><span class="line">MOV R2, #0xFFFFFFFF</span><br><span class="line">MOV R3, #0x00000002</span><br><span class="line">@ 1.2第二个数 0000 0004 0000 0003 0000 0004 0000 0003</span><br><span class="line">MOV R4, #0x00000003</span><br><span class="line">MOV R5, #0x00000004</span><br><span class="line">MOV R6, #0x00000003</span><br><span class="line">MOV R7, #0x00000004</span><br><span class="line">@ 1.3开始加法运算</span><br><span class="line">ADDS R8, R0, R4</span><br><span class="line">ADCS R9, R1, R5</span><br><span class="line">ADCS R10, R2, R6</span><br><span class="line">ADC R11, R3, R7</span><br></pre></td></tr></table></figure>



<h3 id="3-2-减法运算"><a href="#3-2-减法运算" class="headerlink" title="3.2 减法运算"></a><font size=3>3.2 减法运算</font></h3><h4 id="3-2-1-SUB"><a href="#3-2-1-SUB" class="headerlink" title="3.2.1 SUB"></a><font size=3>3.2.1 <code>SUB</code></font></h4><table>
    <tr><td align="center">指令</td><td align="center">计算公式 </td></tr>
    <tr><td align="left">SUB Rd, Rn, Rm</td><td align="left">Rd = Rn - Rm</td></tr>
    <tr><td align="left">SUB Rd, Rn, #immediate</td><td align="left">Rd = Rn - #immediate</td></tr>
    <tr><td align="left">SUB Rd, #immediate</td><td align="left">Rd = Rd - #immediate</td></tr>
    <tr><td align="left">SUB Rd, Rn</td><td align="left">Rd = Rd - Rn</td></tr>
</table>


<p><strong>【指令说明】</strong><code>SUB</code>是<strong>不带借位</strong>的减法指令（前减后）。</p>
<p><strong>【使用实例】</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">MOV R1, #1</span><br><span class="line">MOV R2, #4</span><br><span class="line"></span><br><span class="line">SUB R3, R2, R1  @ R4 = R2 - R1 = 2 - 1 = 1</span><br><span class="line">SUB R2, R1      @ R2 = R2 - R1 = 2 - 1 = 1</span><br><span class="line">SUB R2, #1      @ R2 = R2 - 1 = 2 - 1 = 1</span><br></pre></td></tr></table></figure>

<h4 id="3-2-2-RSB"><a href="#3-2-2-RSB" class="headerlink" title="3.2.2 RSB"></a><font size=3>3.2.2 <code>RSB</code></font></h4><table>
    <tr><td align="center">指令</td><td align="center">计算公式 </td></tr>
    <tr><td align="left">RSB Rd, Rn, Rm</td><td align="left">Rd = Rm - Rn</td></tr>
    <tr><td align="left">RSB Rd, Rn, #immediate</td><td align="left">Rd = #immediate - Rn</td></tr>
</table>


<p><strong>【指令说明】</strong><code>RSB</code>是<strong>逆向</strong>减法指令（后减前）。</p>
<h4 id="3-1-2-SUC"><a href="#3-1-2-SUC" class="headerlink" title="3.1.2 SUC"></a><font size=3>3.1.2 <code>SUC</code></font></h4><table>
    <tr><td align="center">指令</td><td align="center">计算公式 </td></tr>
    <tr><td align="left">SBC Rd, Rn, #immediate</td><td align="left">Rd = Rn - #immediate – 借位</td></tr>
    <tr><td align="left">SBC Rd, Rn, Rm</td><td align="left">Rd = Rn - Rm – 借位</td></tr>
</table>


<p><strong>【指令说明】</strong><code>SUBC</code>是<strong>带借位</strong>的减法指令。</p>
<p><strong>【注意】</strong>进行带借位的减法指令时，若有用到<code>SUB</code>，则需要加上<code>S</code>后缀，这样才会使<code>CPSR</code>进位标志<code>C</code>产生变化，才可以真正实现借位。</p>
<p><strong>【使用实例】</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">@ 8.带借位的减法</span><br><span class="line">@ 8.1第一个数 0000 0002 0000 0001</span><br><span class="line">MOV R0, #0x00000001                  @ 第一个数低32位</span><br><span class="line">MOV R1, #0x00000002                  @ 第一个数高32位</span><br><span class="line">@ 8.2第二个数 0000 0001 0000 0005</span><br><span class="line">MOV R2, #0x00000005                  @ 第二个数低32位</span><br><span class="line">MOV R3, #0x00000001                  @ 第二个数高32位</span><br><span class="line">@ 8.3带借位减法结果  </span><br><span class="line">SUBS R6, R0, R2</span><br><span class="line">SBC R7, R1, R3                       @ (R7 = R1 - R3 - CPSR[!C]) C取反</span><br></pre></td></tr></table></figure>



<h3 id="3-4-乘法运算"><a href="#3-4-乘法运算" class="headerlink" title="3.4 乘法运算"></a><font size=3>3.4 乘法运算</font></h3><h4 id="3-4-1-MUL"><a href="#3-4-1-MUL" class="headerlink" title="3.4.1 MUL"></a><font size=3>3.4.1 <code>MUL</code></font></h4><table>
    <tr><td align="center">指令</td><td align="center">计算公式 </td></tr>
    <tr><td align="left">MUL Rd, Rn, Rm</td><td align="left">Rd = Rn * Rm</td></tr>
</table>


<p><strong>【指令说明】</strong><code>MUL</code>是（<code>32</code>位）乘法指令。</p>
<p><strong>【使用实例】</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">MOV R1, #1</span><br><span class="line">MOV R2, #2</span><br><span class="line"></span><br><span class="line">MUL R3, R1, R2  @ R3 = R1 x R2 = 1 x 2 = 2</span><br></pre></td></tr></table></figure>

<h3 id="3-5-除法运算"><a href="#3-5-除法运算" class="headerlink" title="3.5 除法运算"></a><font size=3>3.5 除法运算</font></h3><p><strong>【注意】</strong><code>ARM</code><strong>不支持除法运算</strong>。</p>
<h4 id="3-5-1-UDIV"><a href="#3-5-1-UDIV" class="headerlink" title="3.5.1 UDIV"></a><font size=3>3.5.1 <code>UDIV</code></font></h4><table>
    <tr><td align="center">指令</td><td align="center">计算公式 </td></tr>
    <tr><td align="left">UDIV Rd, Rn, Rm</td><td align="left">Rd = Rn / Rm</td></tr>
</table>


<p><strong>【指令说明】</strong><code>UDIV</code>是无符号除法指令。</p>
<h4 id="3-5-2-SDIV"><a href="#3-5-2-SDIV" class="headerlink" title="3.5.2 SDIV"></a><font size=3>3.5.2 <code>SDIV</code></font></h4><table>
    <tr><td align="center">指令</td><td align="center">计算公式 </td></tr>
    <tr><td align="left">SDIV Rd, Rn, Rm</td><td align="left">Rd = Rn / Rm</td></tr>
</table>


<p><strong>【指令说明】</strong><code>UDIV</code>是有符号除法指令。</p>
<h2 id="4-逻辑运算指令"><a href="#4-逻辑运算指令" class="headerlink" title="4. 逻辑运算指令"></a><font size=3>4. 逻辑运算指令</font></h2><h3 id="4-1按位与运算"><a href="#4-1按位与运算" class="headerlink" title="4.1按位与运算"></a><font size=3>4.1按位与运算</font></h3><h4 id="4-1-1AND"><a href="#4-1-1AND" class="headerlink" title="4.1.1AND"></a><font size=3>4.1.1<code>AND</code></font></h4><table>
    <tr><td align="center">指令</td><td align="center">计算公式 </td></tr>
    <tr><td align="left">AND Rd, Rn</td><td align="left">Rd = Rd & Rn</td></tr>
    <tr><td align="left">AND Rd, Rn, #immediate</td><td align="left">Rd = Rn & #immediate</td></tr>
    <tr><td align="left">AND Rd, Rn, Rm</td><td align="left">Rd = Rn & Rm</td></tr>
</table>


<p><strong>【指令说明】</strong><code>AND</code>是按位与运算指令。</p>
<p><strong>【使用实例】</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">MOV R1, #1      @ R1 = 0000 0001</span><br><span class="line">MOV R2, #3      @ R1 = 0000 0011</span><br><span class="line">AND R3, R1, R2  @ R3 = 0000 0001</span><br></pre></td></tr></table></figure>

<h3 id="4-2-按位或运算"><a href="#4-2-按位或运算" class="headerlink" title="4.2 按位或运算"></a><font size=3>4.2 按位或运算</font></h3><h4 id="4-2-1-ORR"><a href="#4-2-1-ORR" class="headerlink" title="4.2.1 ORR"></a><font size=3>4.2.1 <code>ORR</code></font></h4><table>
    <tr><td align="center">指令</td><td align="center">计算公式 </td></tr>
    <tr><td align="left">ORR Rd, Rn</td><td align="left">Rd = Rd | Rn</td></tr>
    <tr><td align="left">ORR Rd, Rn, #immediate</td><td align="left">Rd = Rn | #immediate</td></tr>
    <tr><td align="left">ORR Rd, Rn, Rm</td><td align="left">Rd = Rn | Rm</td></tr>
</table>


<p><strong>【指令说明】</strong><code>ORR</code>是按位或运算指令。</p>
<p><strong>【使用实例】</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">MOV R1, #1      @ R1 = 0000 0001</span><br><span class="line">MOV R2, #3      @ R1 = 0000 0011</span><br><span class="line">ORR R4, R1, R2  @ R4 = 0000 0011</span><br></pre></td></tr></table></figure>



<h3 id="4-3-位清除"><a href="#4-3-位清除" class="headerlink" title="4.3 位清除"></a><font size=3>4.3 位清除</font></h3><h4 id="4-3-1-BIC"><a href="#4-3-1-BIC" class="headerlink" title="4.3.1 BIC"></a><font size=3>4.3.1 <code>BIC</code></font></h4><table>
    <tr><td align="center">指令</td><td align="center">计算公式 </td></tr>
    <tr><td align="left">BIC Rd, Rn</td><td align="left">Rd = Rd & (~Rn)</td></tr>
    <tr><td align="left">BIC Rd, Rn, #immediate</td><td align="left">Rd = Rn & (~#immediate)</td></tr>
    <tr><td align="left">BIC Rd, Rn, Rm</td><td align="left">Rd = Rn & (~Rm)</td></tr>
</table>


<p><strong>【指令说明】</strong><code>BIC</code>是位清除指令，第二操作数中哪一位为<code>1</code>，就将第一操作数哪一位清<code>0</code>，然后将结果存入目标寄存器。</p>
<p><strong>【使用实例】</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">MOV R1, #0xAA   @ R1 = 1010 1010</span><br><span class="line">MOV R2, #0x0F   @ R2 = 0000 1111</span><br><span class="line">BIC R3, R1, R2  @ R3 = 1010 0000</span><br></pre></td></tr></table></figure>



<h3 id="4-4-按位异或"><a href="#4-4-按位异或" class="headerlink" title="4.4 按位异或"></a><font size=3>4.4 按位异或</font></h3><h4 id="4-4-1-EOR"><a href="#4-4-1-EOR" class="headerlink" title="4.4.1 EOR"></a><font size=3>4.4.1 <code>EOR</code></font></h4><table>
    <tr><td align="center">指令</td><td align="center">计算公式 </td></tr>
    <tr><td align="left">EOR Rd, Rn</td><td align="left">Rd = Rd ^ Rn</td></tr>
    <tr><td align="left">EOR Rd, Rn, #immediate</td><td align="left">Rd = Rn ^ #immediate</td></tr>
    <tr><td align="left">EOR Rd, Rn, Rm</td><td align="left">Rd = Rn ^ Rm</td></tr>
</table>


<p><strong>【指令说明】</strong><code>EOR</code>是按位异或指令。</p>
<p><strong>【使用实例】</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">MOV R1, #1      @ R1 = 0000 0001</span><br><span class="line">MOV R2, #3      @ R1 = 0000 0011</span><br><span class="line">EOR R5, R1, R2  @ R5 = 0000 0010</span><br></pre></td></tr></table></figure>

<h3 id="4-5-左移"><a href="#4-5-左移" class="headerlink" title="4.5 左移"></a><font size=3>4.5 左移</font></h3><h4 id="4-5-1-LSL"><a href="#4-5-1-LSL" class="headerlink" title="4.5.1 LSL"></a><font size=3>4.5.1 <code>LSL</code></font></h4><table>
    <tr><td align="center">指令</td><td align="center">计算公式 </td></tr>
    <tr><td align="left">LSL Rd, Rn, Rm</td><td align="left">Rd = Rn &lt;&lt; Rm(左移Rm位)</td></tr>
</table>


<p><strong>【指令说明】</strong><code>LSL</code>是左移指令。</p>
<p><strong>【使用实例】</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">MOV R1, #1      @ R1 = 0000 0001</span><br><span class="line">MOV R2, #3      @ R1 = 0000 0011</span><br><span class="line">LSL R6, R1, R2  @ R6 = 0000 1000 (R1 &lt;&lt; R2) </span><br></pre></td></tr></table></figure>

<h3 id="4-6-右移"><a href="#4-6-右移" class="headerlink" title="4.6 右移"></a><font size=3>4.6 右移</font></h3><h4 id="4-6-1-LSR"><a href="#4-6-1-LSR" class="headerlink" title="4.6.1 LSR"></a><font size=3>4.6.1 <code>LSR</code></font></h4><table>
    <tr><td align="center">指令</td><td align="center">计算公式 </td></tr>
    <tr><td align="left">LSR Rd, Rn, Rm</td><td align="left">Rd = Rn  &gt;&gt; Rm(右移Rm位)</td></tr>
</table>


<p><strong>【指令说明】</strong><code>LSR</code>是右移指令。</p>
<p><strong>【使用实例】</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">MOV R1, #1      @ R1 = 0000 0001</span><br><span class="line">MOV R2, #3      @ R1 = 0000 0011</span><br><span class="line">LSR R7, R6, R2  @ R7 = 0000 0001 (R6 &gt;&gt; R2) </span><br></pre></td></tr></table></figure>

<h2 id="5-三种相关寻址实例"><a href="#5-三种相关寻址实例" class="headerlink" title="5. 三种相关寻址实例"></a><font size=3>5. 三种相关寻址实例</font></h2><p>这部分主要是介绍数据处理指令相关的<code>ARM</code>指令寻址方式。</p>
<h3 id="5-1-立即数寻址"><a href="#5-1-立即数寻址" class="headerlink" title="5.1 立即数寻址"></a><font size=3>5.1 立即数寻址</font></h3><p>立即数寻址，也叫立即寻址，寻找数据时直接从机器码中获取。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">MOV R0, #1</span><br><span class="line">ADD R1, R0, #2</span><br></pre></td></tr></table></figure>

<h3 id="5-2-寄存器寻址"><a href="#5-2-寄存器寻址" class="headerlink" title="5.2 寄存器寻址"></a><font size=3>5.2 寄存器寻址</font></h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ADD R2, R1, R0</span><br></pre></td></tr></table></figure>

<h3 id="5-3-寄存器移位寻址"><a href="#5-3-寄存器移位寻址" class="headerlink" title="5.3 寄存器移位寻址"></a><font size=3>5.3 寄存器移位寻址</font></h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">MOV R3, R2, LSL #1      @ 将 R2 左移1位后的结果存入 R3</span><br></pre></td></tr></table></figure>



<h1 id="三、跳转指令"><a href="#三、跳转指令" class="headerlink" title="三、跳转指令"></a><font size=3>三、跳转指令</font></h1><h2 id="1-直接修改PC"><a href="#1-直接修改PC" class="headerlink" title="1. 直接修改PC"></a><font size=3>1. 直接修改<code>PC</code></font></h2><p>我们可以直接修改程序计数器<code>PC</code>的值来实现程序的跳转。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">MOV PC, #0x10</span><br></pre></td></tr></table></figure>

<p>像这样，我们便可以跳转到相应的地址去执行相应的指令了，但是，有一个问题就是我们在写代码的时候并不知道跳转的目标地址是多少。</p>
<h2 id="2-B"><a href="#2-B" class="headerlink" title="2. B"></a><font size=3>2. <code>B</code></font></h2><table>
    <tr><td align="center">指令</td><td align="center">说明</td></tr>
    <tr><td align="left" width=150px>B &lt;label&gt;</td><td align="left">跳转到label，如果跳转范围超过了 +/-2KB，可以指定 B.W &lt;label&gt;使用 32位版本的跳转指令， 这样可以得到较大范围的跳转</td></tr>
</table>


<p><strong>【指令说明】</strong><code>B</code>是不带返回的跳转指令，实现程序的跳转，本质是将<code>PC</code>寄存器的值修改成跳转标号下第一条指令的地址。如果要调用的函数不会再返回到原来的执行处，那就可以用 <code>B</code>指令。</p>
<p><strong>【使用实例】</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">MAIN2:</span><br><span class="line">	MOV R1, #1</span><br><span class="line">	MOV R2, #2</span><br><span class="line">	B FUNC2         @ 不带返回的跳转指令</span><br><span class="line">	MOV R3, #3</span><br><span class="line">	</span><br><span class="line">FUNC2:</span><br><span class="line">	MOV R4, #1</span><br><span class="line">	MOV R5, #2</span><br><span class="line">	MOV R6, #3</span><br></pre></td></tr></table></figure>

<h2 id="3-BL"><a href="#3-BL" class="headerlink" title="3. BL"></a><font size=3>3. <code>BL</code></font></h2><table>
    <tr><td align="center">指令</td><td align="center">说明</td></tr>
    <tr><td align="left" width=150px>BL &lt;label&gt;</td><td align="left">跳转到标号地址，并将返回地址保存在LR中。</td></tr>
</table>


<p><strong>【指令说明】</strong><code>BL</code>是带返回的跳转指令，实现程序的跳转，本质是将<code>PC</code>寄存器的值修改成跳转标号下第一条指令的地址，同时将跳转指令的下一条指令的地址保存到<code>LR</code>寄存器中。需要注意的是，想要返回到跳转之前的位置，还需要将<code>LR</code>寄存器的值给到<code>PC</code>寄存器。</p>
<p><strong>【使用实例】</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">MAIN3:</span><br><span class="line">	MOV R1, #1</span><br><span class="line">	MOV R2, #2</span><br><span class="line">	BL FUNC3        @ 带返回的跳转指令</span><br><span class="line">	MOV R3, #3</span><br><span class="line">	</span><br><span class="line">FUNC3:</span><br><span class="line">	MOV R4, #1</span><br><span class="line">	MOV R5, #2</span><br><span class="line">	MOV R6, #3	</span><br><span class="line">	MOV PC,LR</span><br></pre></td></tr></table></figure>

<h2 id="4-BX"><a href="#4-BX" class="headerlink" title="4. BX"></a><font size=3>4. <code>BX</code></font></h2><table>
    <tr><td align="center">指令</td><td align="center">说明</td></tr>
    <tr><td align="left" width=150px>BX Rm</td><td align="left">带状态切换的跳转指令，若 Rm 的 bit[0] 为1，切换到 Thumb 指令执行；若 Rm 的 bit[0] 为0，切换到 ARM 指令执行。</td></tr>
</table>


<p><strong>【指令说明】</strong><code>BX</code>是带状态切换的跳转指令，此条指令与<code>B</code>一样，是不带返回的。</p>
<p><strong>【使用实例】</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">BX R0 @ 跳转到R0寄存器中存储的地址，如果R0[0]=1，则进入Thumb状态。</span><br></pre></td></tr></table></figure>

<h2 id="5-BLX"><a href="#5-BLX" class="headerlink" title="5. BLX"></a><font size=3>5. <code>BLX</code></font></h2><table>
    <tr><td align="center">指令</td><td align="center">说明</td></tr>
    <tr><td align="left" width=150px>BLX Rm</td><td align="left" rowspan=2>结合 BX和 BL的特点，跳转到 Rm指定的地址，并将返回地址保存在 LR 中，并切换指令集。</td></tr>
    <tr><td align="left" width=150px>BLX &lt;label&gt;</td></tr>
</table>


<p><strong>【指令说明】</strong><code>BLX</code>是带状态切换和带返回的跳转指令。</p>
<p><strong>【注意】</strong></p>
<p>（1）<code>BLX &lt;label&gt;</code>无论何种情况，始终会更改处理器的状态；<code>BLX Rm</code> 可从<code> Rm</code> 的<code>bit[0]</code>推算出目标状态。</p>
<p>（2）如果 <code>Rm</code> 的<code>bit[0]</code> 为 <code>0</code>，则处理器的状态会更改为（或保持在）<code>ARM</code> 状态；如果 <code>Rm</code> 的<code>bit [0]</code> 为 <code>1</code>，则处理器的状态会更改为（或保持在）<code>Thumb</code> 状态。</p>
<h2 id="6-使用实例"><a href="#6-使用实例" class="headerlink" title="6. 使用实例"></a><font size=3>6. 使用实例</font></h2><blockquote>
<p>【题目】使用汇编计算 1 + 2 + 3 + … + 98 + 99 + 100</p>
</blockquote>
<p>【解答】</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">@ ARM指令条件码实现求100以内的正整数之和</span><br><span class="line">@ 1 + 2 +3 + ... + 98 + 99 + 100 = 50 x 101 = 5050 = 13BA</span><br><span class="line"></span><br><span class="line">.text				@ 表示当前段为代码段 </span><br><span class="line">.global _start		@ 声明_start为全局符号</span><br><span class="line">_start:				@ 汇编程序的入口</span><br><span class="line"></span><br><span class="line">	MOV R0, #100    @ R0存储结束的条件</span><br><span class="line">	MOV R1, #0      @ R1存储当前要加的数</span><br><span class="line">FUNC:</span><br><span class="line">	ADD R2, R2, R1  @ R2存储 R2 + R1 的值</span><br><span class="line">	ADD R1, R1, #1  @ R1自增1</span><br><span class="line">	CMP R1, R0      @ 比较R0和R1</span><br><span class="line">	BGT STOP        @ R1 &gt; R0 时,也就是R1 &gt; 100了，循环结束</span><br><span class="line">	BLE FUNC </span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">STOP:				@ 死循环，防止程序跑飞</span><br><span class="line">	B STOP</span><br><span class="line">	</span><br><span class="line">.end				@ 汇编程序的结束</span><br></pre></td></tr></table></figure>



<h1 id="四、Load-Store指令"><a href="#四、Load-Store指令" class="headerlink" title="四、Load/Store指令"></a><font size=3>四、<code>Load/Store</code>指令</font></h1><p><code>ARM</code>不能直接访问存储器，当我们需要使用存储器来存储数据，或者从存储器读取数据的时候，就需要使用内存访问指令来实现对内存的读写。</p>
<h2 id="1-单个寄存器内存访问"><a href="#1-单个寄存器内存访问" class="headerlink" title="1. 单个寄存器内存访问"></a><font size=3>1. 单个寄存器内存访问</font></h2><h3 id="1-1-STR"><a href="#1-1-STR" class="headerlink" title="1.1 STR"></a><font size=3>1.1 <code>STR</code></font></h3><table>
    <tr><td align="center">指令</td><td align="center">说明</td></tr>
    <tr><td align="left">STR Rn, [Rd, #offset]</td><td align="left">将寄存器Rn中的4个字节数据写入到存储器中的 Rd + offset位置。</td></tr>
    <tr><td align="left">STRB Rn, [Rd, #offset]</td><td align="left">将寄存器Rn中的1个字节数据写入到存储器中的 Rd + offset位置。</td></tr>
    <tr><td align="left">STRH Rn, [Rd, #offset]</td><td align="left">将寄存器Rn中的2个字节数据写入到存储器中的 Rd + offset位置。</td></tr>
</table>
**【指令说明】**`STR`指令将寄存器中的数据写入到存储器中（一次性写入`4`个字节），`STRB`可以向存储器中写入`1`个字节数据，`STRH`可以向存储器写入`2`字节的数据。

<p><strong>【注意】</strong></p>
<p>（1）<code>#offset</code>不写的时候，将直接从<code>Rd</code>位置开始存储。</p>
<p>（2）对于小端模式的<code>ARM</code>处理器来说，写入<code>1</code>字节或者<code>2</code>字节的时候，都是从低字节开始写的。</p>
<p><strong>【使用实例】</strong>（下边的例子提前用到了<code>LDR</code>，后边在说明）</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">MOV R0, #0x40000000             @ 要写入的内存地址</span><br><span class="line">MOV R1, #0xFF000000             @ 要写入的数据</span><br><span class="line">STR R1, [R0]                    @ 将R1寄存器中的4字节数据存储到R0指向的内存空间</span><br><span class="line">@ 3.写1个字节到内存</span><br><span class="line">@ MOV R3, 0x12345678            @ 这样是会报错的，立即数的问题，网上说超过8位的立即数最好用LDR</span><br><span class="line">LDR R3, = 0x12345678            @ 将 0x12345678 写入R3寄存器</span><br><span class="line">STRB R3, [R0]                   @ 写一个字节到R0指向的内存空间</span><br><span class="line">@ 4.写2个字节到内存</span><br><span class="line">LDR R4, = 0x12345678            @ 将数据 0x12345678 写入到 R4 寄存器</span><br><span class="line">LDR R5, = 0x40000004            @ 将内存地址 0x40000004 写入到 R5 寄存器</span><br><span class="line">STRH R4, [R5]                   @ 写两个字节到R5指向的内存空间</span><br></pre></td></tr></table></figure>

<h3 id="1-2-LDR"><a href="#1-2-LDR" class="headerlink" title="1.2 LDR"></a><font size=3>1.2 <code>LDR</code></font></h3><h4 id="1-2-1-用法一：读取数据"><a href="#1-2-1-用法一：读取数据" class="headerlink" title="1.2.1 用法一：读取数据"></a><font size=3>1.2.1 用法一：读取数据</font></h4><table>
    <tr><td align="center">指令</td><td align="center">说明</td></tr>
    <tr><td align="left" width = 200px>LDR Rd, [Rn, #offset]</td><td align="left">从存储器地址为 Rd + offset 的位置读取4个字节数据到Rd寄存器。</td></tr>
    <tr><td align="left" width = 200px>LDRB Rd, [Rn, #offset]</td><td align="left">从存储器地址为 Rd + offset 的位置读取1个字节数据到Rd寄存器。</td></tr>
    <tr><td align="left" width = 200px>LDRH Rd, [Rn, #offset]</td><td align="left">从存储器地址为 Rd + offset 的位置读取2个字节数据到Rd寄存器。</td></tr>
</table>
**【指令说明】**`LDR`指令将存储器中的数据读取到寄存器（一次性读取`4`个字节），`LDRB`可以从存储器读取`1`个字节数据，`LDRH`可以从存储器读取`2`字节的数据。

<p><strong>【注意】</strong></p>
<p>（1）<code>#offset</code>不写的时候，将直接从<code>Rd</code>位置开始读取。</p>
<p>（2）对于小端模式的<code>ARM</code>处理器来说，读取<code>1</code>字节或者<code>2</code>字节的时候，也是从低字节开始读的。</p>
<h4 id="1-2-2-用法二：伪指令"><a href="#1-2-2-用法二：伪指令" class="headerlink" title="1.2.2 用法二：伪指令"></a><font size=3>1.2.2 用法二：伪指令</font></h4><table>
    <tr><td align="center">指令</td><td align="center">说明</td></tr>
    <tr><td align="left" width = 200px>LDR Rd, = #data</td><td align="left">此指令并非ARM指令，而是一条伪指令(后边会再学习)，它会将data存储到Rd寄存器，这时候它与MOV很像，只是MOV指令后会有立即数的限制，而LDR不会。</td></tr>
    <tr><td align="left" width = 200px>LDR Rd, &lt;label&gt;</td><td align="left">把label当做地址，然后将label指向的地址中的值加载到寄存器Rd中。</td></tr>
    <tr><td align="left" width = 200px>LDR Rd, = &lt;label&gt;</td><td align="left">把label的值加载到寄存器Rd中。</td></tr>
</table>

<p><strong>【指令说明】</strong><code>LDR</code>在此处用作伪指令用途。</p>
<h3 id="1-3-两种相关寻址方式实例"><a href="#1-3-两种相关寻址方式实例" class="headerlink" title="1.3 两种相关寻址方式实例"></a><font size=3>1.3 两种相关寻址方式实例</font></h3><h4 id="1-3-1-寄存器间接寻址"><a href="#1-3-1-寄存器间接寻址" class="headerlink" title="1.3.1 寄存器间接寻址"></a><font size=3>1.3.1 寄存器间接寻址</font></h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">LDR R4, = 0x40000000</span><br><span class="line">LDR R5, = 0x12345678</span><br><span class="line">STR R5, [R4]            @ 通过寄存器操作内存</span><br></pre></td></tr></table></figure>

<h4 id="1-3-2-寄存器基址变址寻址"><a href="#1-3-2-寄存器基址变址寻址" class="headerlink" title="1.3.2 寄存器基址变址寻址"></a><font size=3>1.3.2 寄存器基址变址寻址</font></h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">LDR R4, = 0x40000000</span><br><span class="line">MOV R5, #4</span><br><span class="line">LDR R6, = 0x12345678</span><br><span class="line">STR R6, [R4, R5]          @ 将数据写入到 R4 + R5 所指向的内存地址，这里就是0x40000004</span><br><span class="line"></span><br><span class="line">STR R6, [R4, R5, LSL #1]  @ 将数据写入到 R4 + (R5 &lt;&lt; 1) 所指向的内存地址，这里就是0x40000008</span><br><span class="line"></span><br><span class="line">STR R6, [R4, #12]         @ 前索引，将数据写入到 R4 + 12 所指向的内存地址，这里就是0x4000000C</span><br><span class="line"></span><br><span class="line">LDR R6, = 0x87654321</span><br><span class="line">STR R6, [R4], #8          @ 后索引，将数据写入到 R4 所指向的内存地址，这里就是0x40000000,然后将R4再加8</span><br><span class="line"></span><br><span class="line">LDR R4, = 0x40000000</span><br><span class="line">LDR R6, = 0x1234ABCD</span><br><span class="line">STR R6, [R4, #8]!         @ 自动索引，将数据写入到 R4 +8 所指向的内存地址，这里就是0x40000008,然后将R4再加8</span><br></pre></td></tr></table></figure>



<h2 id="2-多寄存器内存访问"><a href="#2-多寄存器内存访问" class="headerlink" title="2. 多寄存器内存访问"></a><font size=3>2. 多寄存器内存访问</font></h2><h3 id="2-1-STM"><a href="#2-1-STM" class="headerlink" title="2.1 STM"></a><font size=3>2.1 <code>STM</code></font></h3><table>
    <tr><td align="center">指令</td><td align="center">说明</td></tr>
    <tr><td align="left">STM {cond} Rd{!}, {reglist}{^}</td><td align="left">将寄存器列表reglist中的所有数据写入到 Rd 所指向的存储器的地址中。</td></tr>
</table>

<p><strong>【指令说明】</strong></p>
<ul>
<li><code>STM</code>：表示执行将多个寄存器数据写入存储器操作，是一种数据块传输指令；</li>
<li><code>cond</code>：表示条件码，后边学习到寻址方式的时候会用到；</li>
</ul>
<details class="folding-tag" blue><summary> 点击查看条件码 </summary>
              <div class='content'>
              <table>    <tr><td align="center" width=150px>IA</td><td align="left">Increase After，每次传送后地址加4,其中的寄存器从左到右执行,例如:STMIA R0,{R1,LR} 先存R1,再存LR</td></tr>    <tr><td align="center" width=150px>IB</td><td align="left">Increase Before，每次传送前地址加4,其中的寄存器从左到右执行,例如:STMIA R0,{R1,LR} 先存R1,再存LR</td></tr>    <tr><td align="center" width=150px>DA</td><td align="left">Decrease After，每次传送后地址减4,其中的寄存器从右到左执行,例如:STMDA R0,{R1,LR} 先存LR,再存R1</td></tr>    <tr><td align="center" width=150px>DB</td><td align="left">Decrease Before，每次传送前地址减4,其中的寄存器从右到左执行,例如:STMDA R0,{R1,LR} 先存LR,再存R1</td></tr>    <tr><td align="center" width=150px>FD</td><td align="left">满递减堆栈 (每次传送前地址减4)</td></tr>    <tr><td align="center" width=150px>FA</td><td align="left">满递增堆栈 (每次传送后地址减4)</td></tr>    <tr><td align="center" width=150px>ED</td><td align="left">空递减堆栈 (每次传送前地址加4)</td></tr>    <tr><td align="center" width=150px>EA</td><td align="left">空递增堆栈 (每次传送后地址加4)</td></tr></table>
              </div>
            </details>

<ul>
<li><code>Rd</code>：基址寄存器，表示要将寄存器列表中的数据写入到存储器的目标地址；</li>
<li><code>!</code>：可选后缀，当数据传送完毕之后，将最后的地址写入到基址寄存器（<code>Rn</code>）中</li>
<li><code>reglist</code>：表示需要传输的寄存器的列表，注意要使用花括号<code>&#123;&#125;</code>括起来，当寄存器连续时，可以使用<code>-</code>连接，如<code>R1-R4</code>，当寄存器不连续时，寄存器列表之间用逗号<code>,</code>分隔开，并且寄存器编号顺序不会影响存储到存储器的顺序（小端模式下的<code>ARM</code>中，寄存器编号小的存储到低地址）。</li>
<li><code>^</code>：可选后缀，当指令为<code>LDM</code>且寄存器列表中包含<code>R15</code>，选用该后缀时表示：除了正常的数据传送外，还将<code>SPSR</code>复制到<code>CPSR</code>中。同时，该后缀还表示传入或传出的是用户模式下的寄存器，而不是当前模式下的寄存器。</li>
</ul>
<p><strong>【注意】</strong>不按顺序写寄存器列表的话，可能会有如下警告：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">warning: register range not in ascending order</span><br></pre></td></tr></table></figure>

<p><strong>【使用实例】</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">LDR R0, = 0x40000000</span><br><span class="line">MOV R1, #1</span><br><span class="line">MOV R2, #2</span><br><span class="line">MOV R3, #3</span><br><span class="line">MOV R4, #4</span><br><span class="line">STM R0, &#123;R1-R4&#125;         @ 将R1到R4这四个寄存器中的数据写入到R0寄存器指向的内存地址为起始地址的内存空间中</span><br><span class="line"></span><br><span class="line">STM R0, &#123;R1, R3, R4&#125;</span><br><span class="line"></span><br><span class="line">STM R0!, &#123;R1-R4&#125;        @ 数据存储完毕后，R0自动增加，存多少字节就增加多少字节。在这里就是增加16字节，即0x40000010</span><br></pre></td></tr></table></figure>

<h3 id="2-2-LDM"><a href="#2-2-LDM" class="headerlink" title="2.2 LDM"></a><font size=3>2.2 <code>LDM</code></font></h3><table>
    <tr><td align="center">指令</td><td align="center">说明</td></tr>
    <tr><td align="left">LDM {cond} Rd{!}, {reglist}{^}</td><td align="left">将 Rd 所指向的存储器的地址中的数据依次读取到寄存器列表reglist。</td></tr>
</table>
**【指令说明】**

<ul>
<li><code>LDM</code>：表示执行将存储器中的多个数据读取到多个寄存器操作，是一种数据块传输指令；</li>
<li><code>cond</code>：表示条件码，后边学习到寻址方式的时候会用到；</li>
</ul>
<details class="folding-tag" blue><summary> 点击查看条件码 </summary>
              <div class='content'>
              <table>    <tr><td align="center" width=150px>IA</td><td align="left">Increase After，每次传送后地址加4,其中的寄存器从左到右执行,例如:STMIA R0,{R1,LR} 先存R1,再存LR</td></tr>    <tr><td align="center" width=150px>IB</td><td align="left">Increase Before，每次传送前地址加4,其中的寄存器从左到右执行,例如:STMIA R0,{R1,LR} 先存R1,再存LR</td></tr>    <tr><td align="center" width=150px>DA</td><td align="left">Decrease After，每次传送后地址减4,其中的寄存器从右到左执行,例如:STMDA R0,{R1,LR} 先存LR,再存R1</td></tr>    <tr><td align="center" width=150px>DB</td><td align="left">Decrease Before，每次传送前地址减4,其中的寄存器从右到左执行,例如:STMDA R0,{R1,LR} 先存LR,再存R1</td></tr>    <tr><td align="center" width=150px>FD</td><td align="left">满递减堆栈 (每次传送前地址减4)</td></tr>    <tr><td align="center" width=150px>FA</td><td align="left">满递增堆栈 (每次传送后地址减4)</td></tr>    <tr><td align="center" width=150px>ED</td><td align="left">空递减堆栈 (每次传送前地址加4)</td></tr>    <tr><td align="center" width=150px>EA</td><td align="left">空递增堆栈 (每次传送后地址加4)</td></tr></table>
              </div>
            </details>

<ul>
<li><code>Rd</code>：基址寄存器，表示要将寄存器列表中的数据写入到存储器的目标地址；</li>
<li><code>!</code>：可选后缀，当数据传送完毕之后，将最后的地址写入到基址寄存器（<code>Rn</code>）中</li>
<li><code>reglist</code>：表示需要传输的寄存器的列表，注意要使用花括号<code>&#123;&#125;</code>括起来，当寄存器连续时，可以使用<code>-</code>连接，如<code>R1-R4</code>，当寄存器不连续时，寄存器列表之间用逗号<code>,</code>分隔开，并且寄存器编号顺序不会影响从存储器读取到寄存器的顺序（小端模式下的<code>ARM</code>中，低地址中的数据存储到小编号的寄存器中）。</li>
<li><code>^</code>：可选后缀，当指令为<code>LDM</code>且寄存器列表中包含<code>R15</code>，选用该后缀时表示：除了正常的数据传送外，还将<code>SPSR</code>复制到<code>CPSR</code>中。同时，该后缀还表示传入或传出的是用户模式下的寄存器，而不是当前模式下的寄存器。</li>
</ul>
<p><strong>【注意】</strong>不按顺序写寄存器列表的话，可能会有如下警告：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">warning: register range not in ascending order</span><br></pre></td></tr></table></figure>

<p><strong>【使用实例】</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">LDR R0, = 0x40000000</span><br><span class="line">LDM R0, &#123;R11, R10&#125;</span><br></pre></td></tr></table></figure>

<h3 id="2-3-一种相关寻址方式实例"><a href="#2-3-一种相关寻址方式实例" class="headerlink" title="2.3 一种相关寻址方式实例"></a><font size=3>2.3 一种相关寻址方式实例</font></h3><h4 id="2-3-1-多寄存器寻址"><a href="#2-3-1-多寄存器寻址" class="headerlink" title="2.3.1 多寄存器寻址"></a><font size=3>2.3.1 多寄存器寻址</font></h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">LDR R1, = 0x11112222</span><br><span class="line">LDR R2, = 0x33334444</span><br><span class="line">LDR R3, = 0x55556666</span><br><span class="line">LDR R0, = 0x40000000</span><br><span class="line">@ 1.Increase After (先存储数据后增加地址)从R0指向的内存空间地址开始向高地址存储R1，R2，R3中数据，顺序是 R1-&gt;R2-&gt;R3</span><br><span class="line">STMIA R0, &#123;R1, R2, R3&#125;</span><br><span class="line">@ 2.Increase Before(先增加地址后存储数据)从R0 + 4 指向的内存空间地址开始向高地址存储R1，R2，R3中数据，顺序是 R1-&gt;R2-&gt;R3</span><br><span class="line">LDR R0, = 0x40000020</span><br><span class="line">STMIB R0, &#123;R1, R2, R3&#125; </span><br><span class="line">@ 3.Decrease After (先存储数据后减小地址)从R0指向的内存空间地址开始向低地址存储R1，R2，R3中数据，顺序是 R3-&gt;R2-&gt;R1(正着看的话其实也是R1-&gt;R2-&gt;R3)</span><br><span class="line">LDR R0, = 0x40000040</span><br><span class="line">STMDA R0, &#123;R1, R2, R3&#125; </span><br><span class="line">@ 4.Decrease Before(先减小地址后存储数据)从R0 - 4 指向的内存空间地址开始向低地址存储R1，R2，R3中数据，顺序是 R3-&gt;R2-&gt;R1(正着看的话其实也是R1-&gt;R2-&gt;R3)</span><br><span class="line">LDR R0, = 0x40000060</span><br><span class="line">STMDB R0, &#123;R1, R2, R3&#125; </span><br><span class="line">@ 总的来说，存的时候都是小编号寄存器放在低字节</span><br></pre></td></tr></table></figure>

<h2 id="3-栈"><a href="#3-栈" class="headerlink" title="3. 栈"></a><font size=3>3. 栈</font></h2><h3 id="3-1-栈的概念"><a href="#3-1-栈的概念" class="headerlink" title="3.1 栈的概念"></a><font size=3>3.1 栈的概念</font></h3><p>栈的本质就是一段内存，程序运行时用于保存一些临时数据，如局部变量、函数的参数、返回值、以及程序跳转时需要保护的寄存器等。</p>
<h3 id="3-2-栈的分类"><a href="#3-2-栈的分类" class="headerlink" title="3.2 栈的分类"></a><font size=3>3.2 栈的分类</font></h3><p>先来了解几个概念：</p>
<table>
    <tr><td align="center">增栈</td><td align="left">压栈时栈指针越来越大，出栈时栈指针越来越小</td></tr>
    <tr><td align="center">减栈</td><td align="left">压栈时栈指针越来越大，出栈时栈指针越来越小</td></tr>
    <tr><td align="center">满栈</td><td align="left">栈指针指向最后一次压入到栈中的数据，压栈时需要先移动栈指针到相邻位置然后再压栈</td></tr>
    <tr><td align="center">空栈</td><td align="left">栈指针指向最后一次压入到栈中的数据的相邻位置，压栈时可直接压栈，之后需要将栈指针移动到相邻位置</td></tr>
</table>


<p>这样的话，栈其实可以分为四种：空增（<code>EA</code>）、空减（<code>ED</code>）、满增（<code>FA</code>）、满减（<code>FD</code>）。在<code>ARM</code>处理器一般使用<strong>满减栈</strong>。各种栈的实现其实都是通过<code>STM/LDM</code>两个指令来实现的，只是不同条件码对应不同类型的栈。对于<code>ARM</code>中常用的满减栈，我们使用的指令如下：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">STMDB R0!, &#123;R1-R4&#125;</span><br><span class="line">LDMIA R0!, &#123;R5-R8&#125;</span><br><span class="line"></span><br><span class="line">@ 就等价于下边的写法，而且下边的写法更清晰</span><br><span class="line">STMFD R0!, &#123;R1-R4&#125;</span><br><span class="line">LDMFD R0!, &#123;R5-R8&#125;</span><br></pre></td></tr></table></figure>

<h3 id="3-3-栈的使用"><a href="#3-3-栈的使用" class="headerlink" title="3.3 栈的使用"></a><font size=3>3.3 栈的使用</font></h3><h4 id="3-3-1-叶子函数"><a href="#3-3-1-叶子函数" class="headerlink" title="3.3.1 叶子函数"></a><font size=3>3.3.1 叶子函数</font></h4><p>叶子函数就是不再调用其他函数的函数。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">.text				@ 表示当前段为代码段 </span><br><span class="line">.global _start		@ 声明_start为全局符号</span><br><span class="line">_start:				@ 汇编程序的入口</span><br><span class="line">	@ 初始化栈指针SP，也就是给R13寄存器存上一个栈地址</span><br><span class="line">	LDR SP, = 0x40000020</span><br><span class="line">	@ 叶子函数：不再调用其他函数的函数</span><br><span class="line"></span><br><span class="line">MAIN:</span><br><span class="line">	MOV R1, #3</span><br><span class="line">	MOV R2, #5</span><br><span class="line">	BL FUNC</span><br><span class="line">	ADD R3, R1, R2</span><br><span class="line">	B STOP</span><br><span class="line"></span><br><span class="line">FUNC:                     @ 叶子函数</span><br><span class="line">	STMFD SP!, &#123;R1, R2&#125;   @ 将R1和R2的值压入栈中，保护现场</span><br><span class="line">	MOV R1, #10</span><br><span class="line">	MOV R2, #20</span><br><span class="line">	SUB R3, R2, R1</span><br><span class="line">	LDMFD SP!, &#123;R1, R2&#125;   @ 将R1和R2的值出栈，恢复现场</span><br><span class="line">	MOV PC, LR</span><br><span class="line">	</span><br><span class="line">STOP:				      @ 死循环，防止程序跑飞</span><br><span class="line">	B STOP</span><br><span class="line">	</span><br><span class="line">.end				      @ 汇编程序的结束</span><br></pre></td></tr></table></figure>

<h4 id="3-3-2-非叶子函数"><a href="#3-3-2-非叶子函数" class="headerlink" title="3.3.2 非叶子函数"></a><font size=3>3.3.2 非叶子函数</font></h4><p>非叶子函数就是在函数中还调用了其他的函数。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line">@ 栈的应用2</span><br><span class="line">.text				@ 表示当前段为代码段 </span><br><span class="line">.global _start		@ 声明_start为全局符号</span><br><span class="line">_start:				@ 汇编程序的入口</span><br><span class="line">	@ 初始化栈指针SP，也就是给R13寄存器存上一个栈地址</span><br><span class="line">	LDR SP, = 0x40000020</span><br><span class="line">	@ 非叶子函数：函数中调用了其他函数</span><br><span class="line"></span><br><span class="line">MAIN:</span><br><span class="line">	MOV R1, #3</span><br><span class="line">	MOV R2, #5</span><br><span class="line">	BL FUNC1</span><br><span class="line">	ADD R3, R1, R2</span><br><span class="line">	B STOP</span><br><span class="line"></span><br><span class="line">FUNC1:                        @ 非叶子函数</span><br><span class="line">	STMFD SP!, &#123;R1, R2, LR&#125;   @ 将R1和R2的值压入栈中，保护现场</span><br><span class="line">	MOV R1, #10</span><br><span class="line">	MOV R2, #20</span><br><span class="line">	BL FUNC2</span><br><span class="line">	SUB R3, R2, R1</span><br><span class="line">	LDMFD SP!, &#123;R1, R2, LR&#125;   @ 将R1和R2的值出栈，恢复现场</span><br><span class="line">	MOV PC, LR</span><br><span class="line"></span><br><span class="line">FUNC2:                        @ 叶子函数</span><br><span class="line">	STMFD SP!, &#123;R1, R2&#125;       @ 将R1和R2的值压入栈中，保护现场</span><br><span class="line">	MOV R1, #7</span><br><span class="line">	MOV R2, #8</span><br><span class="line">	MUL R3, R1, R2</span><br><span class="line">	LDMFD SP!, &#123;R1, R2&#125;       @ 将R1和R2的值出栈，恢复现场</span><br><span class="line">	MOV PC, LR</span><br><span class="line">	</span><br><span class="line">STOP:				    @ 死循环，防止程序跑飞</span><br><span class="line">	B STOP</span><br><span class="line">	</span><br><span class="line">.end				@ 汇编程序的结束</span><br></pre></td></tr></table></figure>



<h1 id="五、状态寄存器传送指令"><a href="#五、状态寄存器传送指令" class="headerlink" title="五、状态寄存器传送指令"></a><font size=3>五、状态寄存器传送指令</font></h1><p>前边我们学习了<code>CPSR</code>寄存器，这是<code>ARM</code>的控制寄存器，我们是无法使用<code>MOV</code>指令来修改<code>CPSR</code>的各个位的，我们只能通过状态寄存器传送指令来读写<code>CPSR</code>寄存器。<code>C</code>语言写的语句都不会被编译成状态寄存器传送指令，一般由操作系统内部使用。</p>
<h2 id="1-MRS"><a href="#1-MRS" class="headerlink" title="1. MRS"></a><font size=3>1. <code>MRS</code></font></h2><table>
    <tr><td align="center">指令</td><td align="center">说明</td></tr>
    <tr><td align="left">MRS Rd, CPSR</td><td align="left">CPSR中的数据读取到Rd寄存器。</td></tr>
</table>


<p><strong>【使用实例】</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">MRS R0, CPSR </span><br></pre></td></tr></table></figure>

<h2 id="2-MSR"><a href="#2-MSR" class="headerlink" title="2. MSR"></a><font size=3>2. <code>MSR</code></font></h2><table>
    <tr><td align="center">指令</td><td align="center">说明</td></tr>
    <tr><td align="left">MSR CPSR, Rn</td><td align="left">将Rn中的数据写入CPSR寄存器。</td></tr>
    <tr><td align="left">MSR CPSR, #immediate</td><td align="left">将 immediate 写入CPSR寄存器。</td></tr>
</table>
**【注意】**`User`模式下是无法修改`CPU`工作模式的。

<p><strong>【使用实例】</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">MRS R0, CPSR </span><br></pre></td></tr></table></figure>



<h1 id="六、软中断指令"><a href="#六、软中断指令" class="headerlink" title="六、软中断指令"></a><font size=3>六、软中断指令</font></h1><p>有时候我们需要从用户模式下切换工作模式，但是我们知道在用户模式下是无法切换工作模式的，而软中断异常的产生可以进入<code>SVC</code>模式，所以我们在需要修改<code>ARM</code>工作模式的时候就可以发出一个软中断。</p>
<h2 id="1-SWI"><a href="#1-SWI" class="headerlink" title="1. SWI"></a><font size=3>1. <code>SWI</code></font></h2><table>
    <tr><td align="center">指令</td><td align="center">说明</td></tr>
    <tr><td align="left">SWI{cond} immed_24</td><td align="left">产生一个软中断异常。</td></tr>
</table>
**【指令说明】**`SWI`指令可以产生一个软中断异常，从而使`CPU`进入管理员模式，指令中`immed_24`是一个`24`位立即数。

<blockquote>
<p>SWI指令后面的24立即数是干什么用的呢？</p>
</blockquote>
<p>用户程序通过<code>SWI</code>指令切换到特权模式，进入软中断处理程序，但是软中断处理程序不知道用户程序到底想要做什么，<code>SWI</code>指令后面的<code>24</code>位立即数用来做用户程序和软中断处理程序之间的信号。通过该软中断立即数来区分用户不同操作，执行不同内核函数。</p>
<p><strong>【使用实例】</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">.text				@ 表示当前段为代码段 </span><br><span class="line">.global _start		@ 声明_start为全局符号</span><br><span class="line">_start:				@ 汇编程序的入口</span><br><span class="line"></span><br><span class="line">	@ 异常向量表</span><br><span class="line">	B MAIN          @ 0x00 Reset</span><br><span class="line">	B .             @ 0x02 Undefined instruction</span><br><span class="line">	B SWI_HANDLER   @ 0x08 Software interrupt(SWI)</span><br><span class="line">	B .             @ 0x0C Prefetch Abort</span><br><span class="line">	B .             @ 0x10 Data Abort</span><br><span class="line">	B .             @ 0x14 Reserved</span><br><span class="line">	B .             @ 0x18 IRQ</span><br><span class="line">	B .             @ 0x1C FIQ</span><br><span class="line">	</span><br><span class="line">	@ 应用程序</span><br><span class="line">	</span><br><span class="line">MAIN:</span><br><span class="line">	LDR SP, = 0x40000020  @ 初始化SVC模式下的栈指针</span><br><span class="line">	MSR CPSR, #0x10       @ 修改CPU为User模式</span><br><span class="line">	</span><br><span class="line">	@ 将会初始化User模式下的栈指针</span><br><span class="line">	@ LDR SP, = 0x40000020 </span><br><span class="line">	</span><br><span class="line">	MOV R1, #1</span><br><span class="line">	MOV R2, #2</span><br><span class="line">	SWI #1                @ 软中断指令，触发异常</span><br><span class="line">	ADD R3, R1, R2</span><br><span class="line">	B STOP</span><br><span class="line"></span><br><span class="line">	@ SWI软件中断异常处理</span><br><span class="line">SWI_HANDLER:</span><br><span class="line">	STMFD SP!, &#123;R1, R2, LR&#125;  @ 压栈，保护现场</span><br><span class="line">	MOV R1, #10</span><br><span class="line">	MOV R2, #20</span><br><span class="line">	SUB R3, R2, R1</span><br><span class="line">	LDMFD SP!, &#123;R1, R2, PC&#125;^  @ 出栈，恢复现场，直接将LR读入PC, ^ 表示直接将SPSR值传递给CPSR</span><br><span class="line">	</span><br><span class="line">STOP:				@ 死循环，防止程序跑飞</span><br><span class="line">	B STOP</span><br><span class="line">	</span><br><span class="line">.end				@ 汇编程序的结束</span><br></pre></td></tr></table></figure>



<h1 id="七、协处理器指令"><a href="#七、协处理器指令" class="headerlink" title="七、协处理器指令"></a><font size=3>七、协处理器指令</font></h1><p>重点目前不在这里，这里就简单了解一下。</p>
<h2 id="1-协处理器简介"><a href="#1-协处理器简介" class="headerlink" title="1. 协处理器简介"></a><font size=3>1. 协处理器简介</font></h2><p>协处理器是一种芯片，用于减轻系统微处理器的特定处理任务。例如，数学协处理器可以控制数字处理；图形协处理器可以处理视频绘制。协处理器可以通过一组专门的、提供<code>load-store</code>类型接口的<code>ARM</code>指令来访问。例如协处理器<code>15</code>（<code>CP15</code>），<code>ARM</code>处理器使用协处理器<code>15</code>的寄存器来控制<code>cache</code>、<code>TCM</code>和存储器管理。</p>
<p><code>ARM</code> 微处理器可支持多达 <code>16</code> 个协处理器，用于各种协处理操作，在程序执行的过程中，每个协处理器只执行针对自身的协处理指令，忽略 <code>ARM</code> 处理器和其他协处理器的指令。</p>
<p><code>ARM</code> 的协处理器指令主要用于：<code>ARM</code> 处理器初始化，<code>ARM</code> 协处理器的数据处理操作，以及在<code>ARM</code> 处理器的寄存器和协处理器的寄存器之间传送数据，和在 <code>ARM</code> 协处理器的寄存器和存储器之间传送数据。</p>
<h2 id="2-协处理器指令简介"><a href="#2-协处理器指令简介" class="headerlink" title="2. 协处理器指令简介"></a><font size=3>2. 协处理器指令简介</font></h2><h3 id="2-1-协处理器数据运算指令"><a href="#2-1-协处理器数据运算指令" class="headerlink" title="2.1 协处理器数据运算指令"></a><font size=3>2.1 协处理器数据运算指令</font></h3><h4 id="2-1-1-CDP"><a href="#2-1-1-CDP" class="headerlink" title="2.1.1 CDP"></a><font size=3>2.1.1 <code>CDP</code></font></h4><p>一般格式如下：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">CDP&#123;条件&#125; 协处理器编码, 协处理器操作码1, 目的寄存器, 源寄存器1, 源寄存器2, 协处理器操作码2</span><br></pre></td></tr></table></figure>

<p><strong>【指令说明】</strong><code>CDP</code> 指令用于<code>ARM</code> 处理器通知<code>ARM</code>协处理器执行特定的操作，若协处理器不能成功完成特定的操作，则产生未定义指令异常。其中协处理器操作码<code>1</code> 和协处理器操作码<code>2</code> 为协处理器将要执行的操作，目的寄存器和源寄存器均为协处理器的寄存器，指令不涉及<code>ARM</code> 处理器的寄存器和存储器。</p>
<p><strong>【使用实例】</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">CDP P3, 2, C12, C10, C3, 4 @ 该指令完成协处理器 P3 的初始化</span><br></pre></td></tr></table></figure>

<h3 id="2-2-协处理器存储器访问指令"><a href="#2-2-协处理器存储器访问指令" class="headerlink" title="2.2 协处理器存储器访问指令"></a><font size=3>2.2 协处理器存储器访问指令</font></h3><h4 id="2-2-1-LDC"><a href="#2-2-1-LDC" class="headerlink" title="2.2.1 LDC"></a><font size=3>2.2.1 <code>LDC</code></font></h4><p>一般格式如下：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">LDC&#123;条件&#125;&#123;L&#125; 协处理器编码, 目的寄存器，[源(ARM)寄存器] @ ARM处理器-&gt;协处理器</span><br></pre></td></tr></table></figure>

<p><strong>【指令说明】</strong><code>LDC</code> 指令用于将源寄存器所指向的存储器中的字数据传送到目的寄存器中，若协处理器不能成功完成传送操作，则产生未定义指令异常。其中，<code>&#123;L&#125;</code>选项表示指令为长读取操作，如用于双精度数据的传输。</p>
<p><strong>【注意】</strong><code>[]</code>中的为<code>ARM</code>处理器中的寄存器。</p>
<p><strong>【使用实例】</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">LDC P3, C4, [R0] @ 将 ARM 处理器的寄存器 R0 所指向的存储器中的字数据传送到协处理器 P3 的寄存器 C4 中</span><br></pre></td></tr></table></figure>

<h4 id="2-2-2-STC"><a href="#2-2-2-STC" class="headerlink" title="2.2.2 STC"></a><font size=3>2.2.2 <code>STC</code></font></h4><p>一般格式如下：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">STC&#123;条件&#125;&#123;L&#125; 协处理器编码, 源寄存器, [目的(ARM)寄存器] @ 协处理器-&gt;ARM处理器</span><br></pre></td></tr></table></figure>

<p><strong>【指令说明】</strong><code>STC</code> 指令用于将源寄存器中的字数据传送到目的寄存器所指向的存储器中，若协处理器不能成功完成传送操作，则发生未定义指令反常。其中，<code>&#123;L&#125;</code>选项表明指令为长读取操作，如用于双精度数据的传输。</p>
<p><strong>【注意】</strong><code>[]</code>中的为<code>ARM</code>处理器中的寄存器。</p>
<p><strong>【使用实例】</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">STC P3, C4, [R0]  @ 将协处理器 P3 的寄存器 C4 中的字数据传送到 ARM 处理器的寄存器 R0 所指向的存储器中</span><br></pre></td></tr></table></figure>

<h3 id="2-3-协处理器寄存器访问指令"><a href="#2-3-协处理器寄存器访问指令" class="headerlink" title="2.3 协处理器寄存器访问指令"></a><font size=3>2.3 协处理器寄存器访问指令</font></h3><p>在基于<code>ARM</code>的嵌入式应用系统中，存储系统的操作通常是由协处理器<code>CP15</code>完成的。<code>CP15</code>包含<code>16</code>个<code>32</code>位的寄存器，其编号为<code>0～15</code>。而访问<code>CP15</code>寄存器的指令主要是<code>MCR</code>和<code>MRC</code>这两个指令。</p>
<h4 id="2-3-1-MRC"><a href="#2-3-1-MRC" class="headerlink" title="2.3.1 MRC"></a><font size=3>2.3.1 <code>MRC</code></font></h4><p>一般格式如下：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">MRC&#123;条件&#125; 协处理器编码, 协处理器操作码1, 目的寄存器, 源寄存器1, 源寄存器2, 协处理器操作码2 @ 读协处理器寄存器</span><br><span class="line">@ 一般用于 CP15 协处理器，使用格式如下：</span><br><span class="line">MRC&#123;cond&#125; P15, &lt;Opcode_1&gt;, &lt;Rd&gt;, &lt;CRn&gt;, &lt;CRm&gt;, &lt;Opcode_2&gt;</span><br></pre></td></tr></table></figure>

<p><strong>【指令说明】</strong><code>MRC</code> 指令用于将协处理器寄存器中的数据传送到<code>ARM</code> 处理器寄存器中，若协处理器不能成功完成操作，则产生未定义指令异常。其中协处理器操作码<code>1 </code>和协处理器操作码<code>2</code>为协处理器将要执行的操作，目的寄存器为<code>ARM</code> 处理器的寄存器，源寄存器<code>1</code> 和源寄存器<code>2 </code>均为协处理器的寄存器。</p>
<p><strong>【参数说明】</strong></p>
<ul>
<li><code>cond</code>：为指令执行的条件码，当<code>cond</code>忽略时指令为无条件执行。</li>
<li><code>Opcode_1</code>：协处理器的特定操作码，对于<code>CP15</code>寄存器来说，<code>opcode1=0</code>。</li>
<li><code>Rd</code>：作为源寄存器的<code>ARM</code>寄存器，将协处理器寄存器的值传送到该寄存器里面 ，通常为<code>R0</code>。</li>
<li><code>CRn</code>：作为目标寄存器的协处理器寄存器，其编号是<code>C0 ~ C15</code>。</li>
<li><code>CRm</code>：协处理器中附加的目标寄存器或源操作数寄存器。如果不需要设置附加信息，将<code>CRm</code>设置为<code>c0</code>，否 则结果未知。</li>
<li><code>Opcode_2</code>：可选的协处理器特定操作码。（用来区分同一个编号的不同物理寄存器，当不需要提供附加信息时，指定为<code>0</code>）。</li>
</ul>
<p><strong>【使用实例】</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">MRC P3, 3, R0, C4, C5, 6   @ 将协处理器 P3 的寄存器 C4 和 C5 中的数据传送到 ARM 处理器寄存器 R0 中</span><br><span class="line">MRC P15, 0, R0, C1, C0, 0  @ 将 CP15 的寄存器 C1 的值读到 r0 中 </span><br></pre></td></tr></table></figure>



<h4 id="2-3-2-MCR"><a href="#2-3-2-MCR" class="headerlink" title="2.3.2 MCR"></a><font size=3>2.3.2 <code>MCR</code></font></h4><p>一般格式如下：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">MCR&#123;条件&#125; 协处理器编码, 协处理器操作码1, 源寄存器, 目的寄存器1, 目的寄存器2, 协处理器操作码2 @ 写协处理器寄存器</span><br><span class="line"></span><br><span class="line">@ 一般用于 CP15 协处理器，使用格式如下：</span><br><span class="line">MCR&#123;cond&#125; P15, &lt;Opcode_1&gt;, &lt;Rd&gt;, &lt;CRn&gt;, &lt;CRm&gt;, &lt;Opcode_2&gt;</span><br></pre></td></tr></table></figure>

<p><strong>【指令说明】</strong><code>MCR</code> 指令用于将 <code>ARM</code> 处理器寄存器中的数据传送到协处理器寄存器中，若协处理器不能成功完成操作，则产生未定义指令异常。其中协处理器操作码<code>1</code> 和协处理器操作码 <code>2</code> 为协处理器将要执行的操作，源寄存器为 <code>ARM</code> 处理器的寄存器，目的寄存器<code>1</code>和目的寄存器 <code>2</code> 均为协处理器的寄存器。</p>
<p><strong>【参数说明】</strong></p>
<ul>
<li><code>cond</code>：为指令执行的条件码，当<code>cond</code>忽略时指令为无条件执行。</li>
<li><code>Opcode_1</code>：协处理器的特定操作码，对于<code>CP15</code>寄存器来说，<code>opcode1=0</code>。</li>
<li><code>Rd</code>：作为源寄存器的<code>ARM</code>寄存器，其值将被传送到协处理器寄存器中，通常为<code>R0</code>。</li>
<li><code>CRn</code>：作为目标寄存器的协处理器寄存器，其编号是<code>C0 ~ C15</code>。</li>
<li><code>CRm</code>：协处理器中附加的目标寄存器或源操作数寄存器。如果不需要设置附加信息，将<code>CRm</code>设置为<code>c0</code>，否 则结果未知。</li>
<li><code>Opcode_2</code>：可选的协处理器特定操作码。（用来区分同一个编号的不同物理寄存器，当不需要提供附加信息时，指定为<code>0</code>）。</li>
</ul>
<p><strong>【使用实例】</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">MCR P3, 3, R0, C4, C5, 6   @ 将 ARM 处理器寄存器 R0 中的数据传送到协处理器 P3 的寄存器 C4 和 C5 中</span><br><span class="line">MCR P15, 0, R0, C8, C7, 0  @ 使无效整个数据 TLB 和指令 TLB2</span><br></pre></td></tr></table></figure>

<h1 id="八、伪指令"><a href="#八、伪指令" class="headerlink" title="八、伪指令"></a><font size=3>八、伪指令</font></h1><h2 id="1-NOP"><a href="#1-NOP" class="headerlink" title="1. NOP"></a><font size=3>1. <code>NOP</code></font></h2><table>
    <tr><td align="center">指令</td><td align="center">说明</td></tr>
    <tr><td align="left">NOP</td><td align="left">空操作</td></tr>
</table>
**【指令说明】**`NOP`指令是一个空操作，它什么也不做。

<h2 id="2-LDR"><a href="#2-LDR" class="headerlink" title="2. LDR"></a><font size=3>2. <code>LDR</code></font></h2><p>当我们使用<code>LDR</code>指令来将一个数据存储到寄存器时，它就属于一个伪指令了。</p>
<table>
    <tr><td align="center">指令</td><td align="center">说明</td></tr>
    <tr><td align="left" width = 200px>LDR Rd, = #data</td><td align="left">此指令并非ARM指令，而是一条伪指令(后边会再学习)，它会将data存储到Rd寄存器，这时候它与MOV很像，只是MOV指令后会有立即数的限制，而LDR不会。</td></tr>
    <tr><td align="left" width = 200px>LDR Rd, &lt;label&gt;</td><td align="left">把label当做地址，然后将label指向的地址中的值加载到寄存器Rd中。</td></tr>
    <tr><td align="left" width = 200px>LDR Rd, = &lt;label&gt;</td><td align="left">把label的值加载到寄存器Rd中。</td></tr>
</table>
**【指令说明】**`LDR`在此处用作伪指令用途。

<p><strong>【使用实例】</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">LDR R1, = 0x12345678    @ 这是一条伪指令，可以将一个任意的32位数据放到寄存器</span><br><span class="line"></span><br><span class="line">LDR R1, = STOP          @ 将STOP的地址写入到寄存器</span><br><span class="line">LDR R1, STOP            @ 将STOP地址中的内容写入到寄存器</span><br><span class="line">	</span><br><span class="line">STOP:				     @ 死循环，防止程序跑飞</span><br><span class="line">	B STOP</span><br></pre></td></tr></table></figure>

<h1 id="九、伪操作"><a href="#九、伪操作" class="headerlink" title="九、伪操作"></a><font size=3>九、伪操作</font></h1><p>前边已经简单了解了伪操作的概念，这里我们来学习一下常见的伪操作。需要注意的是，<code>GNU</code>中的伪操作一般都以<code>.</code>开头。</p>
<h2 id="1-byte"><a href="#1-byte" class="headerlink" title="1. .byte"></a><font size=3>1. <code>.byte</code></font></h2><p>定义单字节数据。例如，</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">.byte 0x12</span><br></pre></td></tr></table></figure>

<h2 id="2-short"><a href="#2-short" class="headerlink" title="2. .short"></a><font size=3>2. <code>.short</code></font></h2><p>定义双字节数据。例如，</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">.short 0x1234</span><br></pre></td></tr></table></figure>

<h2 id="3-long"><a href="#3-long" class="headerlink" title="3. .long"></a><font size=3>3. <code>.long</code></font></h2><p>定义四字节数据。例如，</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">.byte 0x12345678</span><br></pre></td></tr></table></figure>

<h2 id="4-equ"><a href="#4-equ" class="headerlink" title="4. .equ"></a><font size=3>4. <code>.equ</code></font></h2><p>相当于<code>C</code>语言中的宏定义，它不会生成机器码，该语句的语法格式如下：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">.equ 变量名, 表达式</span><br></pre></td></tr></table></figure>

<p><strong>【使用实例】</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">.equ DATA, 0x12  @ 表示 num = 0x12。</span><br><span class="line">MOV R0, #DATA</span><br></pre></td></tr></table></figure>

<h2 id="5-align"><a href="#5-align" class="headerlink" title="5. .align"></a><font size=3>5. <code>.align</code></font></h2><p>数据字节对齐方式，语法格式为：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">.align n   @ n表示对齐的字节数</span><br></pre></td></tr></table></figure>

<p><strong>【使用实例】</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">.align 4   @ 4字节对齐</span><br></pre></td></tr></table></figure>

<h2 id="6-end"><a href="#6-end" class="headerlink" title="6. .end"></a><font size=3>6. <code>.end</code></font></h2><p>表示源汇编文件结束，语法格式为：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">.end      @ 表示源文件结束</span><br></pre></td></tr></table></figure>

<h2 id="7-global"><a href="#7-global" class="headerlink" title="7. .global"></a><font size=3>7. <code>.global</code></font></h2><p>定义一个全局符号，语法格式为：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">.global symbol @ 声明 symbol 为全局符号</span><br></pre></td></tr></table></figure>

<p><strong>【使用实例】</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">.global _start		@ 声明_start为全局符号</span><br></pre></td></tr></table></figure>

<h2 id="8-local"><a href="#8-local" class="headerlink" title="8. .local"></a><font size=3>8. <code>.local</code></font></h2><p>定义一个局部符号，只能在当前文件使用，语法格式为：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">.local symbol		    @ 声明 symbol 为局部符号，只能在当前文件使用 n   @ n表示对齐的字节数</span><br></pre></td></tr></table></figure>

<h2 id="9-weak"><a href="#9-weak" class="headerlink" title="9. .weak"></a><font size=3>9. <code>.weak</code></font></h2><p>定义一个局部符号，只能在当前文件使用，语法格式为：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">.weak symbol		    @ 弱化一个符号</span><br></pre></td></tr></table></figure>

<p><strong>【使用实例】</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">.weak main</span><br><span class="line">B main</span><br></pre></td></tr></table></figure>

<h2 id="10-word"><a href="#10-word" class="headerlink" title="10. .word"></a><font size=3>10. <code>.word</code></font></h2><p>在当前地址空间申请一个字的空间并初始化，语法格式为：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">.word value</span><br></pre></td></tr></table></figure>

<p><strong>【使用实例】</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">.word 0xFFFFFFFF</span><br></pre></td></tr></table></figure>

<h2 id="11-space"><a href="#11-space" class="headerlink" title="11. .space"></a><font size=3>11. <code>.space</code></font></h2><p>在当前地址空间申请多个字节的空间并初始化，语法格式为：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">.space n, value @ 在当前地址空间申请 n 个字节的空间并初始化为value</span><br></pre></td></tr></table></figure>

<p><strong>【使用实例】</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">.space 12, 0x12.word 0xFFFFFFFF</span><br></pre></td></tr></table></figure>

<h2 id="12-if"><a href="#12-if" class="headerlink" title="12. .if "></a><font size=3>12. <code>.if </code></font></h2><p>条件编译，这与<code>C</code>语言中的条件编译效果一样，语法格式为：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">.if &lt;expression&gt;  @ 0 or 1</span><br><span class="line">	...           @ 语句</span><br><span class="line">.endif            @ 结束标志</span><br></pre></td></tr></table></figure>

<p><strong>【使用实例】</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">.if 0</span><br><span class="line">    MOV R3, #3</span><br><span class="line">    MOV R4, #4</span><br><span class="line">.endif</span><br></pre></td></tr></table></figure>

<h2 id="13-macro"><a href="#13-macro" class="headerlink" title="13. .macro"></a><font size=3>13. <code>.macro</code></font></h2><p>类似于<code>C</code>语言中的定义函数，语法格式为：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">.macro name</span><br><span class="line">	...</span><br><span class="line">.endm</span><br></pre></td></tr></table></figure>

<p><strong>【使用实例】</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">.macro FUNC</span><br><span class="line">		MOV R1, #1</span><br><span class="line">		MOV R2, #2</span><br><span class="line">.endm</span><br><span class="line">FUNC</span><br></pre></td></tr></table></figure>

<h2 id="14-rept"><a href="#14-rept" class="headerlink" title="14. .rept"></a><font size=3>14. <code>.rept</code></font></h2><p>此伪操作会生成多次相同的机器码，语法格式为：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">.rept num</span><br><span class="line">	...</span><br><span class="line">.endr</span><br></pre></td></tr></table></figure>

<p><strong>【使用实例】</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">.rept 3                    @ 编译3次，下边两条将会生成6次机器码</span><br><span class="line">	MOV R5, #5</span><br><span class="line">	MOV R6, #6</span><br><span class="line">.endr</span><br></pre></td></tr></table></figure>



<h1 id="十、C与汇编混合编程"><a href="#十、C与汇编混合编程" class="headerlink" title="十、C与汇编混合编程"></a><font size=3>十、<code>C</code>与汇编混合编程</font></h1><h2 id="1-汇编中调用C"><a href="#1-汇编中调用C" class="headerlink" title="1. 汇编中调用C"></a><font size=3>1. 汇编中调用<code>C</code></font></h2><p>我们可以在汇编语言中调用（跳转）<code>C</code>语言，需要注意的是，在汇编中<code>C</code>语言中的函数将会被当做标号来处理。</p>
<div class="tabs" id="tabname1"><ul class="nav-tabs"><li class="tab active"><a href="#tabname1-1">arm-asm.s</a></li><li class="tab"><a href="#tabname1-2">main.c</a></li></ul><div class="tab-content"><div class="tab-pane active" id="tabname1-1"><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">.text				@ 表示当前段为代码段 </span><br><span class="line">.global _start		@ 声明_start为全局符号</span><br><span class="line">_start:				@ 汇编程序的入口</span><br><span class="line">	MOV R1, #1</span><br><span class="line">	MOV R2, #2</span><br><span class="line">	@ 1.汇编语言调用(跳转)C语言</span><br><span class="line">	BL func_c</span><br><span class="line">	MOV R3, #3</span><br><span class="line">	</span><br><span class="line">STOP:				@ 死循环，防止程序跑飞</span><br><span class="line">	B STOP</span><br><span class="line">	</span><br><span class="line">.end				@ 汇编程序的结束</span><br></pre></td></tr></table></figure></div><div class="tab-pane" id="tabname1-2"><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">func_c</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> a = <span class="number">3</span>;</span><br><span class="line">	a++;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div></div></div>

<h2 id="2-C中调用汇编"><a href="#2-C中调用汇编" class="headerlink" title="2. C中调用汇编"></a><font size=3>2. <code>C</code>中调用汇编</font></h2><p>我们可以在<code>C</code>语言中调用（跳转）汇编，需要注意的是，在<code>C</code>语言中，汇编的函数被当做函数处理。</p>
<div class="tabs" id="tabname2"><ul class="nav-tabs"><li class="tab active"><a href="#tabname2-1">arm-asm.s</a></li><li class="tab"><a href="#tabname2-2">main.c</a></li></ul><div class="tab-content"><div class="tab-pane active" id="tabname2-1"><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">.text				@ 表示当前段为代码段 </span><br><span class="line">.global _start		@ 声明_start为全局符号</span><br><span class="line">_start:				@ 汇编程序的入口</span><br><span class="line">	MOV R1, #1</span><br><span class="line">	MOV R2, #2</span><br><span class="line">	@ 1.汇编语言调用(跳转)C语言</span><br><span class="line">	BL func_c</span><br><span class="line">	MOV R3, #3</span><br><span class="line">	</span><br><span class="line">.global FUNC_ASM</span><br><span class="line">FUNC_ASM:</span><br><span class="line">	MOV R4, #4</span><br><span class="line">	</span><br><span class="line">STOP:				@ 死循环，防止程序跑飞</span><br><span class="line">	B STOP</span><br><span class="line">	</span><br><span class="line">.end				@ 汇编程序的结束</span><br></pre></td></tr></table></figure></div><div class="tab-pane" id="tabname2-2"><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">func_c</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> a = <span class="number">3</span>;</span><br><span class="line">	a++;</span><br><span class="line">	FUNC_ASM();</span><br><span class="line">	a--;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div></div></div>

<h2 id="3-C内嵌汇编"><a href="#3-C内嵌汇编" class="headerlink" title="3. C内嵌汇编"></a><font size=3>3. <code>C</code>内嵌汇编</font></h2><p>我们可以在<code>C</code>语言中直接执行汇编指令，使用的格式为：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">asm</span>(<span class="string">&quot;ARM 汇编指令\n&quot;</span>);</span><br></pre></td></tr></table></figure>



<div class="tabs" id="tabname3"><ul class="nav-tabs"><li class="tab active"><a href="#tabname3-1">arm-asm.s</a></li><li class="tab"><a href="#tabname3-2">main.c</a></li></ul><div class="tab-content"><div class="tab-pane active" id="tabname3-1"><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">.text				@ 表示当前段为代码段 </span><br><span class="line">.global _start		@ 声明_start为全局符号</span><br><span class="line">_start:				@ 汇编程序的入口</span><br><span class="line">	MOV R1, #1</span><br><span class="line">	MOV R2, #2</span><br><span class="line">	@ 1.汇编语言调用(跳转)C语言</span><br><span class="line">	BL func_c</span><br><span class="line">	MOV R3, #3</span><br><span class="line">	</span><br><span class="line">STOP:				@ 死循环，防止程序跑飞</span><br><span class="line">	B STOP</span><br><span class="line">	</span><br><span class="line">.end				@ 汇编程序的结束</span><br></pre></td></tr></table></figure></div><div class="tab-pane" id="tabname3-2"><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">func_c</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> a = <span class="number">3</span>;</span><br><span class="line">	a++;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">asm</span></span><br><span class="line">	(</span><br><span class="line">		<span class="string">&quot;MOV R6, #6\n&quot;</span></span><br><span class="line">		<span class="string">&quot;MOV R7, #7\n&quot;</span></span><br><span class="line">	);</span><br><span class="line">	FUNC_ASM();</span><br><span class="line">	a--;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div></div></div>

<h2 id="4-ATPCS协议"><a href="#4-ATPCS协议" class="headerlink" title="4. ATPCS协议"></a><font size=3>4. <code>ATPCS</code>协议</font></h2><h3 id="4-1-ATPCS简介"><a href="#4-1-ATPCS简介" class="headerlink" title="4.1 ATPCS简介"></a><font size=3>4.1 <code>ATPCS</code>简介</font></h3><p><code>ATPCS</code>，即<code>ARM-Thumb Procedure Call Standard</code>，翻译过来就是<code>ARM-Thumb</code>过程调用标准。为了使单独编译的<code>C</code>语言程序和汇编程序之间能够相互调用，必须为子程序之间的调用规定一定的规则。<code>ATPCS</code>就是<code>ARM</code>程序和<code>THUMB</code>程序中子程序调用的基本规则。</p>
<h3 id="4-2-ATPCS主要内容"><a href="#4-2-ATPCS主要内容" class="headerlink" title="4.2 ATPCS主要内容"></a><font size=3>4.2 <code>ATPCS</code>主要内容</font></h3><h4 id="4-2-1-寄存器使用规则"><a href="#4-2-1-寄存器使用规则" class="headerlink" title="4.2.1 寄存器使用规则"></a><font size=3>4.2.1 寄存器使用规则</font></h4><ul>
<li><code>R15</code>：程序计数器，只能用于存储程序的指针，不能作为其他用途；</li>
<li><code>R14</code>：连接寄存器，只能用于存储返回地址，不能用作其他用途；</li>
<li><code>R13</code>：栈指针，只能用于存储栈指针，不能用作其他用途；</li>
<li><code>R0-R3</code>：当函数的参数少于<code>4</code>个的时候使用<code>R0-R3</code>传参，多出4个的参数部分使用栈传递函数返回值使用<code>R0</code>传递;</li>
<li><code>R4-R12</code>：主要用于存储局部变量。</li>
</ul>
<h4 id="4-2-2-堆栈规则"><a href="#4-2-2-堆栈规则" class="headerlink" title="4.2.2 堆栈规则"></a><font size=3>4.2.2 堆栈规则</font></h4><p><code>ATPCS</code>规定堆栈为<code>FD</code>类型，即满递减堆栈。并且堆栈的操作是<code>8</code>字节对齐。而对于汇编程序来说,如果目标文件中包含了外部调用,则必须满足以下条件:</p>
<ul>
<li>外部接口的数据栈一定是<code>8</code>位对齐的，也就是要保证在进入该汇编代码后，直到该汇编程序调用外部代码之间,数据栈的栈指针变化为偶数个字;</li>
<li>在汇编程序中使用<code>PRESERVE8</code>伪操作告诉连接器，本汇编程序是<code>8</code>字节对齐的.</li>
</ul>
<h4 id="4-2-3-参数传递规则"><a href="#4-2-3-参数传递规则" class="headerlink" title="4.2.3 参数传递规则"></a><font size=3>4.2.3 参数传递规则</font></h4><p>根据参数个数是否固定,可以将子程序分为参数个数固定的子程序和参数个数可变的子程序。这两种子程序的参数传递规则是不同的。</p>
<ul>
<li>参数个数可变的子程序参数传递规则</li>
</ul>
<p>对于参数个数可变的子程序,当参数不超过<code>4</code>个时，可以使用寄存器<code>R0~R3</code>来进行参数传递，当参数超过<code>4</code>个时,还可以使用数据栈来传递参数。</p>
<p>在参数传递时，将所有参数看做是存放在连续的内存单元中的字数据。然后，依次将各名字数据传送到寄存器<code>R0,R1,R2,R3</code>; 如果参数多于<code>4</code>个，将剩余的字数据传送到数据栈中，入栈的顺序与参数顺序相反，即最后一个字数据先入栈。</p>
<p>按照上面的规则，一个浮点数参数可以通过寄存器传递,也可以通过数据栈传递,也可能一半通过寄存器传递，另一半通过数据栈传递。</p>
<ul>
<li>参数个数固定的子程序参数传递规则</li>
</ul>
<p>对于参数个数固定的子程序，如果系统包含浮点运算的硬件部件，参数传递与参数个数可变的子程序参数传递规则不同。浮点参数将按照下面的规则传递:</p>
<p>（1）各个浮点参数按顺序处理;<br>（2）为每个浮点参数分配FP寄存器;<br>（3）分配的方法是：满足该浮点参数需要的且编号最小的一组连续的<code>FP</code>寄存器。第一个整数参数通过寄存器<code>R0~R3</code>来传递,其他参数通过数据栈传递。</p>
<h4 id="4-2-4-子程序结果返回规则"><a href="#4-2-4-子程序结果返回规则" class="headerlink" title="4.2.4 子程序结果返回规则"></a><font size=3>4.2.4 子程序结果返回规则</font></h4><ul>
<li>结果为一个<code>32</code>位的整数时，可以通过寄存器<code>R0</code>返回。</li>
<li>结果为一个<code>64</code>位整数时，可以通过<code>R0</code>和<code>R1</code>返回，依此类推。</li>
<li>结果为一个浮点数时，可以通过浮点运算部件的寄存器<code>f0</code>，<code>d0</code>或者<code>s0</code>来返回。</li>
<li>结果为一个复合的浮点数时，可以通过寄存器<code>f0-fN</code>或者<code>d0~dN</code>来返回。</li>
<li>对于位数更多的结果，需要通过调用内存来传递。</li>
</ul>

    </div>

    
    
    

    <footer class="post-footer">




    <div>
        
            <div style="text-align:center;color: #ccc;font-size:14px;">
            ----------本文结束
            <i class="fas fa-fan fa-spin" style="color: #FF1493; font-size: 1rem"></i>
            感谢您的阅读----------
            </div>
        
    </div>





  
  <div class="my_post_copyright"> 
    <p><span>文章标题:</span><a href="/post/aaf5ec56.html">LV08-01-ARM体系-03-ARM汇编基础</a></p>
    <p><span>文章作者:</span><a href="/" title="欢迎访问 《苏木》 的学习笔记">苏木</a></p>
    <p><span>发布时间:</span>2023年07月06日 - 22:59</p>
    <p><span>最后更新:</span>2025年06月14日 - 00:25</p>
    <p><span>原始链接:</span><a href="/post/aaf5ec56.html" title="LV08-01-ARM体系-03-ARM汇编基础">https://sumumm.github.io/post/aaf5ec56.html</a></p>
    <p><span>许可协议:</span><i class="fab fa-creative-commons"></i> <a rel="license" href= "https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
  </div>
  


          <div class="post-tags">
              <a href="/tags/LV08-ARM%E5%9F%BA%E7%A1%80/" rel="tag"><i class="fa fa-tag"></i> LV08-ARM基础</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/post/5ef50cda.html" rel="prev" title="LV09-01-环境搭建-01-TFTP与NFS">
                  <i class="fa fa-angle-left"></i> LV09-01-环境搭建-01-TFTP与NFS
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/post/c994fcf6.html" rel="next" title="LV08-01-ARM体系-02-ARM基础知识">
                  LV08-01-ARM体系-02-ARM基础知识 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2017 – 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">苏木</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">3.7m</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">225:26</span>
  </span>
</div>




    <span id="sitetime"></span>
    <script defer language=javascript>
        function siteTime()
        {
            window.setTimeout("siteTime()", 1000);
            var seconds = 1000;
            var minutes = seconds * 60;
            var hours = minutes * 60;
            var days = hours * 24;
            var years = days * 365;
            var today = new Date();
            var todayYear = today.getFullYear();
            var todayMonth = today.getMonth()+1;
            var todayDate = today.getDate();
            var todayHour = today.getHours();
            var todayMinute = today.getMinutes();
            var todaySecond = today.getSeconds();
            /*==================================================
            Date.UTC() -- 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳)
            year        - 作为date对象的年份，为4位年份值
            month       - 0-11之间的整数，做为date对象的月份
            day         - 1-31之间的整数，做为date对象的天数
            hours       - 0(午夜24点)-23之间的整数，做为date对象的小时数
            minutes     - 0-59之间的整数，做为date对象的分钟数
            seconds     - 0-59之间的整数，做为date对象的秒数
            microseconds - 0-999之间的整数，做为date对象的毫秒数
            ==================================================*/
            var t1 = Date.UTC(2017, 
                              5, 
                              19, 
                              0, 
                              0, 
                              0); //北京时间
            var t2 = Date.UTC(todayYear,todayMonth,todayDate,todayHour,todayMinute,todaySecond);
            var diff = t2-t1;
            var diffYears = Math.floor(diff/years);
            var diffDays = Math.floor((diff/days)-diffYears*365);
            var diffHours = Math.floor((diff-(diffYears*365+diffDays)*days)/hours);
            var diffMinutes = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours)/minutes);
            var diffSeconds = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours-diffMinutes*minutes)/seconds);
            document.getElementById("sitetime").innerHTML="已在这里 "+diffYears+" 年 "+diffDays+" 天 "+diffHours+" 小时 "+diffMinutes+" 分钟 "+diffSeconds+" 秒";
        }
        siteTime();
    </script>



    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.umd.js" integrity="sha256-ytMJGN3toR+a84u7g7NuHm91VIR06Q41kMWDr2pq7Zo=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>

  <script src="/js/third-party/pace.js"></script>


  




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


 
        <div id="click-show-text"
            data-mobile = false
            data-text = 富强,民主,文明,和谐,自由,平等,公正,法制,爱国,敬业,诚信,友善
            data-fontsize = 15px
            data-random= false>
        </div>
       

      
        <script async src=https://cdn.jsdelivr.net/npm/hexo-next-mouse-effect@latest/click/showText.js></script>
      

      
    




    <script async src="/js/fancybox_param.js"></script>





<!-- APlayer本体 -->



</body>
</html>
